Problem with C ++ forward declaration when calling a method

I have a problem that I think is related to forward ads, but maybe not.

Here is the relevant code:

hijras

#ifndef A_H_
#define A_H_

#include "B.h"

class A
{
    private:
        B b;

    public:
        A() : b(*this) {}

        void bar() {}
};

#endif /*A_H_*/

Bh

#ifndef B_H_
#define B_H_

#include "A.h"

class A;

class B
{
    private:
        A& a;

    public:
        B(A& a) : a(a) {}

        void foo() { /*a.bar();*/ } //doesn't compile
};

#endif /*B_H_*/

main.cpp

#include "A.h"

int main()
{
    A a;

    return 0;
}

The problem is calling A :: bar (). The program compiles successfully until I try to call this method, after which I get two errors:

error: invalid use of incomplete type 'struct A

error: forward declaration 'struct A

, , A:: bar() , . , A , . ++, , , . . , !

+3
6

, B.h. - :

B.h:

#ifndef B_H_
#define B_H_

// don't include A.h here!

class A;

class B
{
   private:
      A& a;

   public:
      B(A& a) : a(a) {}

      void foo();
};

#endif /*B_H_*/

B.cpp:

#include "B.h"
#include "A.h"

void B::foo() { a.bar(); } // now you're ok

: , :

B A, . - , , heck A - , - . A.h( .cpp ), A , , .

, . , , . , main.cpp, :

// #include "A.h" ==>
#define A_H_

// #include "B.h" ==>
#define B_H_

// #include "A.h" ==> nothing happens! (since A_H_ is already defined)

class A;

class B {
private:
    A& a;

public:
    B(A& a) : a(a) {}

    void foo() { a.bar(); } // <-- what the heck is A here?
                            //     it not defined until below
};

class A {
private:
   B b;

public:
   A() : b(*this) {}

   void bar() {}
};

int main() {
    A a;
    return 0;
}
+9

#include<file.h> file.h. , main.cpp, , . , A:: bar(), .

// result from #include "A.h"

#ifndef A_H_
#define A_H_

// Now, #include "B.h" in A.h will get you the following
#ifndef B_H_
#define B_H_

// here you include "A.h" again, but now it has no effect
// since A_H_ is already defined

class A;

class B
{
    private:
            A& a;
    public:
            B(A& a) : a(a) {}
            // Oops, you want to use a.bar() but it is not defined yet
            void foo() { /*a.bar();*/ } 
};

#endif /*B_H_*/

class A
{
    private:
            B b;
    public:
            A() : b(*this) {}
            void bar() {}
};
#endif /*A_H_*/

// now this is your main function
int main()
{
    A a;
    return 0;
}
+1

, . " ". , , , ( , , , , ), - .

, , . ( , ), , , . , , , , , .

, : Data DataAnalyzer

DataAnalyzer ( ), DataAnalyzer Data ( ) - ! , DataAnalyzer ( ++, ), /, DataAnalyzer. :

class IAnalyzer
{
public:
    virtual void Analyze () = 0;
};

DataAnalyzer, :

class DataAnalyzer : public IAnalyzer
{
public:
    DataAnalyzer (Data* data);

    virtual void Analyze (); // defined in DataAnalyzer.cpp
};

:

class Data
{
public:
    Data ();

    IAnalyzer* Analyzer;
};

- - :

void main ()
{
    Data*  data = new Data ();
    data->Analyzer = new DataAnalyzer (data);
}

( , IAnalyzer Data), DataAnalyzer . , DataAnalyzer Data, .

: , .

!

+1

, B:: foo , B.h, .

B.h:

#ifndef B_H_
#define B_H_

// Forward declaration of A.
class A;

class B
{
private:
    A& a;

public:
    B(A& a) : a(a) {}

    void foo();
};

// Include definition of A after definition of B.
#include "A.h"

inline void B::foo()
{
    a.bar();
}

#endif /*B_H_*/

:

// Include definition of B before definition of A.
// This must be done before the ifndef A_H_ include sentry,
// otherwise A.h cannot be included without including A.h first.
#include "B.h"

#ifndef A_H_
#define A_H_

class A
{
private:
    B b;

public:
    A() : b(*this) {}

    void bar() {}
};

#endif /*A_H_*/
+1

B.h A.h, , A.

B.h B.h B.cpp .

PS . A.h B.h, . , ;)

0

To add to another answer (a circular link, which is the correct answer), if you are building with C # / Java, you understand that C ++ is different, they parse this file in order (instead of treating it as a whole). Therefore, you need to be careful that everything is defined before it is used, in the order that the files are included (and / or individual functions in the .cpp files, respectively).

0
source

All Articles