Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Unknown pointer size when forward declaring (error C2036)

In a header file, I have forward declared two members of a namespace:

namespace Foo {
    struct Odp; 
    typedef std::vector<Odp> ODPVEC;
};

class Bar
{
public:
     Foo::ODPVEC baz; // C2036
};

The error generated by the compiler is:

error C2036: 'Foo::Odp *': unknown size

I'm guessing this is an issue with forward declaring Odp. How can I get around this?

like image 775
Nick Heiner Avatar asked Dec 28 '25 16:12

Nick Heiner


2 Answers

Don't forward declare Odp. The compiler does not know what the type of std::vector<Odp> is, because Odp isn't yet declared. Give the compiler a full declaration for that class.

like image 86
Billy ONeal Avatar answered Dec 30 '25 05:12

Billy ONeal


std::vector requires full type declaration of the first template parameter because it stores objects by value, not by pointer, and thus requires knowledge of object size. You might get away with forward declaration if you store pointers in the vector, like:

class foo;
typedef std::vector<foo*> foo_ptr_vec;

See the fine documentation for gory details.

like image 42
Nikolai Fetissov Avatar answered Dec 30 '25 06:12

Nikolai Fetissov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!