2

This question is somewhat related but still doesn't solves my problem. Consider two classes A and B:

class A{
public:
    A(int id, B* coordinator);
    virtual ~A();
    B *coordinator;
}
class B{
public:
    B(int id, int param2);
    virtual ~B();
    run();
}

void b::run(){
    while(true){
      A(1, this);
      A(2, this);
      //something else create which may create more A
    }
}

int main(int argc, char* argv[]) {
    B bObj(atoi(argv[1]), atoi(argv[2]));
}

In main function I create an object of class B type called as bObj. Constructor of B calls run() and B.run() goes in an infinite loop. Where it creates multiple objects of A. Now I want to pass a pointer of bObj to newly created objects of class A, so that they can access other public variables and functions of bObj. So I am passing this pointer while creating objects of A from B. And I have declared a class variable coordinator of B* type in class A. But I am getting this: Compiler error -> ISO C++ forbids declaration of ‘B’ with no type.

Please help me with this. I am fairly new to C++.

2 Answers 2

1

Did you try forward declaration of class B? That should atleast partially solve your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

I got it solved I was including B.h in A. That was creating the problem. I removed that and its compiling. But now when I am calling the functions of B from A using the pointer. I am getting these two errors: forward declaration of ‘struct B’ & invalid use of incomplete type ‘struct B' . I have shared whole code here: coliru.stacked-crooked.com/a/017ec9516add9e7e
1

In your current code, the compiler sees the entity B in your constructor, but doesn't know it as a type since it is yet to encounter such type declaration.

To solve your problem, You should simply forward declare B. And remember the ; semi-colon at the end of each class definition

class B;                        //forward declaration

class A{
public:
    A(int id, B* coordinator);
    virtual ~A();
    B *coordinator;
};                              //remember the semi-colon!

6 Comments

Thanks! Where should I do this declaration?
Above the declaration of the class A. See the snippet in my answer
Thanks, I did that. But Now I am getting error everywhere where I have declared variables of type A. Like there is a vector in B which contains instances of A created in B. When I create vector<A> allAs;. I get error as "template argument1 is invalid". I am initializing B* passed to A in A's constructors' initializers list.
Can you post a fully working code? perhaps on coliru.stacked-crooked.com and click share, then copy the site link and paste here
I got it solved I was including B.h in A. That was creating the problem. I removed that and its compiling. But now when I am calling the functions of B from A using the pointer. I am getting these two errors: forward declaration of ‘struct B’ & invalid use of incomplete type ‘struct B' . I will share the whole code soon.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.