C++: Has Not Been Declared

后端 未结 2 1682
余生分开走
余生分开走 2020-12-10 19:42

I have seen this type of error everywhere and, although I have looked at the answers, none seem to help.

I get the following error with the following piece of code:<

相关标签:
2条回答
  • 2020-12-10 20:25

    Replace the include with a forward declaration:

    //B.h
    class A;
    class B{
        public:
             static bool doX(A *a);
    };
    

    Include files only when you have to.

    Also, use include guards. This will prevent other nasty issues like re-definitions & such.

    0 讨论(0)
  • 2020-12-10 20:34

    If you have two headers including each other you end up with a circular dependency, and due to the way the preprocessor works it means one will be defined before the other.

    To fix, I would avoid including A.h in B.h, and just forward declare instead:

    class A;
    class B{
        public:
             static bool doX(A *a);
    };
    

    You can then include A.h in B.cpp

    0 讨论(0)
提交回复
热议问题