cppclean icon indicating copy to clipboard operation
cppclean copied to clipboard

Cpp clean tells me I need to #include something that I can forward declare.

Open ChopsII opened this issue 10 years ago • 5 comments

Consider the following:

class Blah;
class Foo: public Bar{
public:
    Foo();
    virtual bool doSomething(Blah* blah);
};

Cppclean thinks that I need to #include "blah.h" even though I'm only using it in pointer form: "Foo.h:1: 'Blah' forward declared, but needs to be #included"

The code compiles fine with the forward declare.

ChopsII avatar Mar 20 '15 03:03 ChopsII

I can't reproduce this:

$ cat Foo.h
class Blah;
class Foo: public Bar{
public:
    Foo();
    virtual bool doSomething(Blah* blah);
};
$ ./cppclean Foo.h

myint avatar Mar 20 '15 13:03 myint

My examples wasn't good. I'll need to look further into this to work out what's going on, but may not have time. My project is very large so it could be very difficult to reduce it to a Minimal, Complete, and Verifiable example Is there a chance that cppclean could give the reason behind its conclusions? Like, it says something needs to be #included, can it give the location of the code that requires the #include, as well as the location of the forward-declare that it gives now?

ChopsII avatar Mar 23 '15 00:03 ChopsII

I've got this kind of behaviour in specific templates. Ex :

class Data;

class Toto {
QPointer<Data> _socket
}

QPointer stores a pointer, it doesn't need access to Data implementation. This is the same for many smart pointers classes.

nmoreaud avatar Oct 26 '17 09:10 nmoreaud

Would something that was forward declared but needs to be included even compile?

Chaosmeister avatar Oct 30 '17 16:10 Chaosmeister

The following might help.

This works:

class MyCoolClass;

int do_something(MyCoolClass * ptr);

but this raises 'MyCoolClass' forward declared, but needs to be #included:

class MyCoolClass;

template <typename T>
T do_something(MyCoolClass * ptr)
{
    // whatever
    return 0;
}

romintomasetti avatar Nov 30 '20 14:11 romintomasetti