Forward declaration error in C++ [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a class defined in a hpp file which I'm trying to use in another header, so I made a forward declaration to it (I only want to use it by reference as a function parameter). For some reason I keep getting a compile error because of it. I can't figure out why it's not working. Here are my codes:
//something.hpp:
class MyClass;
void someFunction (MyClass& mc);
...
//something.cpp:
#include "MyClass.hpp"
void someFunction (MyClass& mc) {...}
...
//MyClass.hpp:
class MyClass {
const char* myText;
public:
MyClass (const char* text) : myText(text) {}
};
//main.cpp:
int main () {
...
someFunction (MyClass ("some text here"));
...
}
And I get an error from main() which says:
'<function-style-cast>' : cannot convert from 'const char [15]' to 'MyClass'
Source or target has incomplete type
If I understand it right, it means that the compiler doesn't find the definition of MyClass, only the predeclaration of it (even though I included MyClass.hpp in something.cpp), that's why it says it's incomplete. What did I miss here?

The function
void someFunction (MyClass& mc);
is accepting a non-const reference to a MyClass instance.
You are not allowed to pass a temporary to such a function.
The code should be
#include "MyClass.hpp"
...
int main() {
MyClass m("some text here");
someFunction(m);
}
or alternatively you need to accept a const reference.
void someFunction (const MyClass& mc);

I don't think that the problem is related to the forward declaration, but rather that you cannot pass a temporary to a function taking a reference as argument. In fact, if you put all your definitions in a single file, it still doesn't compile. On the other hand, if you replace the code of the main with the following, it will compile:
int main() {
MyClass mc("Some text here");
someFunction(mc);
}

Related

Why can a const function not be called on a const object in this case? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
Hey I'm trying to look at the basics of OOP and I do not understand why in this particular instance, I cannot call a member function which is defined const on an object which is also const. But since both are deemed const, I dont change behavior for any of the objects or their attributes right? What does this mean? VS code says that the object has type qualifiers that are incompatible with the member function g. Any information will be appreciated. Thank you!
class D
{
private:
double x;
public:
D()
{
x = 2.0;
}
const double &g() {
return x;
}
};
int main()
{
const D d;
d.g();
}
You placed the const in the wrong place. D::g is not a const-member function. It is a non-const member function (that returns a const reference). You want:
const double& g() const {
//^^ const member function
//^---------^ return type
return x;
}

Why can't I access the object using the arrow operator [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Suppose I have
struct B
{
template<typename T>
void doSomething(T& bar);
};
class A
{
//... (ctor etc.)
template<typename T>
void foo(const std::string& name, T& bar)
{
for(auto& data : myData)
{
if(data.first == name)
*(data.second)->doSomething(bar); //ERROR
}
}
private:
std::vector<std::pair<std::string, B**>> myData;
};
I thought this would work because we are iterating over 'myData' by reference, i.e. no copies, and then we do *(data.second) to access a pointer to B, which we then use the '> operator to access its 'oSomething Method.
Instead, I am just getting the error:
member reference base type 'B' is not a structure or union*
which makes no sense to me since B* is a pointer to a struct, and using the '->' operator on that would dereference the pointer revealing the actual class.
GodBolt Reproduction: https://godbolt.org/z/c45oMT
And the error mentioned in the question is because *(data.second)->doSomething(bar) is really the same as *(data.second->doSomething(bar)).
That is you try to call data.second->doSomething(bar) which is not possible because data.second is a pointer to a pointer to an object.
Then you attempt to dereference whatever doSomething returns, which also isn't possible since it doesn't return anything.
What you probably want is something like
(*data.second)->doSomething(bar);
This will first dereference data.second removing one indirection, resulting in a pointer to B (i.e. *data.second results in a B*). Then call the function on that object.

i can't get a friend member function to actually be able to access private members [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I was reading about friendship in c++(i thought i actually understood it), but when I go to the source code to try it out within some classes, i just don't manage to get it going. I wish to be able to understand WHY it isn't working.
I've already done some research within this website and some others, and i actually found some code that worked, but i really can't see how the logic i'm trying to implement is different from the it:https://www.geeksforgeeks.org/friend-class-function-cpp/
struct B;
struct A{
A(int _a): a(_a){}
friend void B::showA(A&);
private:
int a;
};
struct B{
void showA(A&);
};
void B::showA(A& _param){
cout << _param.a;
}
I expect the function void B::showA(A&) to be able to access the private member "a" of class A, but when i try to compile my code it produces these errors:
friendshipninheritance.cpp(10): error C2027: use of undefined type 'B'
friendshipninheritance.cpp(5): note: see declaration of 'B'
friendshipninheritance.cpp(21): error C2248: 'A::a': cannot access private
member declared in class 'A'
friendshipninheritance.cpp(12): note: see declaration of 'A::a'
friendshipninheritance.cpp(7): note: see declaration of 'A'
Just reorder declarations.
struct A;
struct B{
void showA(A&);
};
struct A{
A(int _a): a(_a){}
friend void B::showA(A&);
private:
int a;
};
void B::showA(A& _param){
cout << _param.a;
}
Structure A must know the name of the member of the structure B. That is, the definition of B must precede the definition of A so that the name showA is known.
As a rule of a thumb, you should solve compiler errors from top. Usually, one error can spawn more errors, and it's not different in this case.
Your friend declaration was ignored, because compiler doesn't yet know what B is and whether or not it has any function called showA. This lead to all further errors.
You can change the order of declarations to make it work:
struct A;
struct B{
void showA(A&);
};
struct A{
A(int _a): a(_a){}
friend void B::showA(A&);
private:
int a;
};
void B::showA(A& _param){
cout << _param.a;
}

error: getting a passing "***" as "this" .... discards qualifiers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The compiler is telling me I can't make the assignment in the constructor. I have extremely similar code in another class and it compiles just fine.
Dependency.h
class Dependency {
public:
Dependency(std::function<void ()> const & func);
private:
std::function<void ()> const call_back;
};
Dependency::Dependency(std::function<void ()> const & func){
call_back = func;
}
Dependency::Dependency(std::function<void ()> const & func){
call_back = func;
}
First, this constructs a Dependency object, and it's call_back member. Then it enters the {}, which attempts to reassign func to call_back, which isn't allowed, because call_back is const.
Instead, we have to tell the compiler to construct call_back with func the first time, which uses this magic syntax:
Dependency::Dependency(std::function<void ()> const & func)
:call_back(func)
{
}
You use the same technique to call a specific constructor for parent classes. Please note that it ignores the order you use here, the compiler will instead always construct the parent class(es) first, and then the members in the order they were declared in the class definition.
A const member can't be assigned after initialization. You have to ensure it is properly initialized in the constructor initialization list.
Dependency::Dependency(std::function<void()> const& func) : call_back(func) {}

Why do I get segmentation fault when I try to access member data of outer class from nested class? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a class Outer with a nested class Inner. Inner has a method 'print' which tries to access a data member of class Outer. The code compiles fine but I get a segmentation fault error when I try to run the programme.
#include <iostream>
class Outer{
public:
Outer();
~Outer();
class Inner{
public:
Inner(Outer *parent){}
void print(){std::cout<< parent-> data <<std::endl;}
private:
Outer *parent;
};
Inner *obj;
private:
int data;
};
Outer::Outer(): data(99), obj(new Inner(this)){}
Outer::~Outer(){delete obj;}
int main(){
Outer outer;
outer.obj->print();
return 0;
}
I'm not sure what is the issue as I have already passed the 'this' pointer to the inner object.
The code is compiled with g++4.8.2 using the c++11 flag.
You are not storing the pointer to the Outer class in the Inner constructor:
Inner(Outer *parent) : parent(parent) {}
Also, if you use C++11, this may be a good case to use smart pointers.
You must turn on compiler warnings and pay attention to them. In this case, the compiler is able to pinpoint the exact error in:
Inner(Outer *parent){}
warning: unused parameter 'parent' [-Wunused-parameter] In constructor 'Outer::Outer()':
(The exact warning message may differ, but all major compilers are able to detect this if you don't prevent them from doing so by using some low default warning level.)
As you leave the parent member variable in an uninitialised status, the following line invokes undefined behaviour because you try to read from the variable:
void print(){std::cout<< parent-> data <<std::endl;}
You must initialise the member variable:
Inner(Outer *parent) : parent(parent) {}
That's not the only problem, though. There's also this:
18:7: warning: 'Outer::data' will be initialized after [-Wreorder]
15:10: warning: 'Outer::Inner* Outer::obj' [-Wreorder]
21:1: warning: when initialized here [-Wreorder]
You can fix it by simply changing the order as follows:
Outer::Outer(): obj(new Inner(this)), data(99) {}
You are not doing anything with parent in
Inner(Outer *parent){}
You need to Inners parent member in the constructor
Inner(Outer *parent_) : parent(parent_){}