Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward Declaration of class in C++, incomplete type

Tags:

c++

I have an issue with Forward Declaration in C++ using clang compiler. Here is my code. It points data in CReference member as incomplete type. Please Help

class Internal;

class CReference {
private:
    Internal data;
public:

    CReference () {}    
    ~CReference (){}
};

class Internal {
public:
    Internal () {}
    ~Internal () {}
};
like image 906
tejusadiga2004 Avatar asked May 31 '13 12:05

tejusadiga2004


2 Answers

Forward declarations are useful when the compiler does not need the complete definition of the type. In other words, if you change your Internal data; to Internal* data or Internal& data, it will work.

Using Internal data;, the compiler needs to know the whole definition of Internal, to be able to create the structure of CReference class.

like image 194
Kiril Kirov Avatar answered Oct 10 '22 02:10

Kiril Kirov


Forward declaration only allows you to use pointers and references to it, until full declaration is available

like image 29
Andrew Avatar answered Oct 10 '22 03:10

Andrew