compiler cannot compile inheritance c++ [duplicate] - c++

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 6 years ago.
Suppose i have a header which contain these two classes
class A:public class B{
// code
};
class B
{
protected:
A a_object;
};
when the compiler compiles this include file,when it comes to Class A ,it sees class A inherits from B but it doesn't reach to Class B definition.So it gives an error. and if i reverse the order of both classes,it gives error due to a_object as it doesn't see Class A definition.
So how to solve this problem? and assume that i restricted to this include file to have class A and B definitions.
Thanks

Here is a simplified version of your problem:
class X {
X x;
};
A class cannot embed an object of its own type.
Specifically, your class B embeds an object of type A, which by inheritance is also of type B.

If you really have to have a hierarchy like that you can do something like the following.
class A;
class B
{
protected:
A* a_object;
};
class A: public B {
// code
};

Related

Decay to base class [duplicate]

This question already has answers here:
Get base class for a type in class hierarchy
(4 answers)
Closed 3 years ago.
Is there a trait that returns the base class of a specific class, with the assumption that there is no multiple inheritance involved? Basically something like:
struct Base
{
};
struct Derived : public Base
{
};
struct DerivedDerived : public Derived
{
};
static_assert(std::is_same_v<base<DerivedDerived>::type,Derived>);
static_assert(std::is_same_v<base<Derived>::type,Base>);
static_assert(std::is_same_v<base<Base>::type,Base>);
// with levels
static_assert(std::is_same_v<base<0,DerivedDerived>::type,Base>);
static_assert(std::is_same_v<base<1,DerivedDerived>::type,Derived>);
static_assert(std::is_same_v<base<2,DerivedDerived>::type,DerivedDerived>);
static_assert(std::is_same_v<base<0,Derived>::type,Base>);
static_assert(std::is_same_v<base<1,Derived>::type,Derived>);
Nope. You can test whether a given type inherits from a given other type with std::is_base_of, but not ask for the base type outright. That is, until C++ gets static reflection sometime in the future.

C++: How to create two classes using each other? [duplicate]

This question already has answers here:
How to create two classes in C++ which use each other as data?
(2 answers)
C++ Circular Dependency in Header Files
(1 answer)
Closed 5 years ago.
I want to create two classes using each other. This is the code:
class B;
class A {
public:
A(B* b) : b(b) {}
void Foo() {
b->data++;
}
B* b;
};
class B {
public:
void Boo() {
A a(this);
a.Foo();
}
int data = 0;
};
int main()
{
B b;
b.Boo();
}
When I compile it in visual Studio 2015, I got the error:
error C2027: use of undefined type 'B'
which points to line six: b->data++;
I have read the problem here, but I have used forward declarations so A should know the existence of B. Also, I don't "have two classes directly contain objects of the other type" because only B contains A but A does not contain B; it uses a pointer to B. Also, my code doesn't seem essentially different from the answer here since there the World class contains objects of Agent and the two classes use each other. But my code just can't compile. So I think my issue is not duplicate. Could you please help me work around this mutual-use issue? Thanks a lot.

How can I create two different classes that have references to eachother within their code? [duplicate]

This question already has answers here:
Two classes that refer to each other
(2 answers)
Closed 8 years ago.
How can I create two different classes that have references to eachother within their code in C++?
It would look something like this;
class A
{
B b;
}
class B
{
A a;
}
I get an error on the line of the
B b;
within class A stating;
error: 'B' was not declared in this scope
Rest easy in that I am not declaring a
B b = new B;
in A, nor am I declaring a
A a = new A;
in B, as I'm aware it would cause a StackOverflowException. I will use getters and setters to manipulate the data.
Use forward declarations, and make the members pointers. It's easiest with two different headers, although it could be done in one:
// A.h
class B;
class A
{
B* m_pB;
};
And:
// B.h
class A;
class B
{
A* m_pA;
};
Then #include both headers in each .cpp file (A.cpp and B.cpp).
This is probably what you want:
class B;
class A
{
B* b;
}
class B
{
A* a;
}
Note that the class B is forward-declared as a class so that the compiler knows what it is when defining A. Also note that A::b is a pointer to B and B::a is a pointer to A (what you probably mean by "reference" if you come from a Java background). If A::b was declared to be of type B and B::a was declared to be of type A then each A object would literally contain an entire B object inside it and vice versa, which I'm guessing is not what you wanted (and is obviously impossible anyway).

C++ inheritance+overloading [duplicate]

This question already has answers here:
C++ inheritance, base methods hidden
(2 answers)
Closed 8 years ago.
In the following code:
struct X{
void stream(int){}
};
struct Y : public X{
void stream(int, int){}
};
int main()
{
Y y;
y.stream(2);
}
Why X::stream(int) is not inherited?
Or it is hided with Y::stream(int, int). If so, why it hidden, not overridden?
Names in derived classes do indeed hide identical names in base classes. This is deliberate. If the base class changes, you don't suddenly and silently want to see a different overload set in your derived class.
To unhide base names explicitly, add using X::stream; into your derived class.

does not name a type [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Resolve circular dependencies in c++
What is forward declaration in c++?
I have two classes A and B. I need to have a field in each which is a pointer to an object of the other class. I get "does not name a type" since the definition of the class is yet to appear. For example:
class A{
B* b;
}
class B{
A* a;
}
gets me "B "does not name a type" at the second line.
Use forward declarations:
class B;
class A {
B* b;
};
class B {
A* a;
};
This way you're telling the compiler that B is going to be declared later on and that it should not worry. See this for more info: Forward declaration
Forward declarations is key to you question , here is the link
What is forward declaration in c++?