cppclean icon indicating copy to clipboard operation
cppclean copied to clipboard

Wrong report of header file "does not need to be included; use a forward declaration instead"

Open omichel opened this issue 9 years ago • 0 comments

I have the following include files: A.hpp

class A {
public:
  A();
  int value() { return mValue; }
private:
  int mValue;
};

B.hpp

#include "A.hpp"

class B {
public:
  B();
  int getAValue() { return mA->value(); }
private:
  A *mA;
};

And here is what cppclean tells me:

$ cppclean B.hpp
B.hpp:1: 'A.hpp' does not need to be #included; use a forward declaration instead

However if I replace #include "A.cpp" with class A;, it doesn't compile anymore:

$ gcc B.hpp
B.hpp: In member function 'int B::getAValue()':
B.hpp:6:30: error: invalid use of incomplete type 'class A'
   int getAValue() { return mA->value(); }
                              ^
B.hpp:2:7: note: forward declaration of 'class A'
 class A;
       ^

omichel avatar Mar 04 '16 15:03 omichel