PDA

View Full Version : problem with forward declaration



MarkoSan
6th January 2008, 13:21
Hi to all!

I have some .h file with:


class ForwardDeclarationClass; // forward declaration of class

In same header file there is:

class MyClass : public QGlWidget
{
....
private:
ForwardDeclarationClass* d;
}

In the .cpp file I have:
...
...
...
class ForwardDeclarationClass
{
....
....
}

and after that there are defined all corresponding methods (in same .cpp file).

I try to compile this Qt project and I get following error:
forward declaration of `struct ForwardDeclarationClass'

What is wrong?

jacek
6th January 2008, 13:27
Could you post the whole error message?

MarkoSan
6th January 2008, 13:34
Here is it:
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'

Namely:
- CMerchandizeBrowser is MyClass
- CMerchandizeBrowserPrivate is ForwardDeclarationClass

jpn
6th January 2008, 13:37
I suspect you're using a non-pointer or non-reference in the header. In this case a forward declaration is insufficient. Could this be the case? What does CMerchandizeBrowser.h:34 contain?

MarkoSan
6th January 2008, 13:41
Here is line:
class CMerchandizeBrowserPrivate; // forward declarationAnd later in the same file in the class CMerchandizeBrowser I have:
private:
CMerchandizeBrowserPrivate* d;

jpn
6th January 2008, 14:30
But is that line 34? As you can see from the error message, it comes from line 34.

MarkoSan
6th January 2008, 14:51
Like I said, the line 34, where error occurs, is:


class CMerchandizeBrowserPrivate; // forward declaration

jacek
6th January 2008, 15:20
Is that the only error message you get? What happens if you comment out that line #34?

MarkoSan
6th January 2008, 15:55
I get several errors after that:
CMerchandizeBrowser.cpp: In member function `int CMerchandizeBrowser::slideCount() const':
CMerchandizeBrowser.cpp:372: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::setSlideCount(int)':
CMerchandizeBrowser.cpp:377: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `QSize CMerchandizeBrowser::slideSize() const':
CMerchandizeBrowser.cpp:382: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::setSlideSize(QSize)':
CMerchandizeBrowser.cpp:387: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `int CMerchandizeBrowser::zoomFactor() const':
CMerchandizeBrowser.cpp:392: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::setZoomFactor(int)':
CMerchandizeBrowser.cpp:397: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `virtual QImage CMerchandizeBrowser::slide(int) const':
CMerchandizeBrowser.cpp:402: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `virtual void CMerchandizeBrowser::setSlide(int, const QImage&)':
CMerchandizeBrowser.cpp:407: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `virtual void CMerchandizeBrowser::setSlide(int, const QPixmap&)':
CMerchandizeBrowser.cpp:412: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `int CMerchandizeBrowser::currentSlide() const':
CMerchandizeBrowser.cpp:417: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::setCurrentSlide(int)':
CMerchandizeBrowser.cpp:422: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::clear()':
CMerchandizeBrowser.cpp:427: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::render()':
CMerchandizeBrowser.cpp:432: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::showPrevious()':
CMerchandizeBrowser.cpp:438: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::showNext()':
CMerchandizeBrowser.cpp:443: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `void CMerchandizeBrowser::showSlide(int)':
CMerchandizeBrowser.cpp:448: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `virtual void CMerchandizeBrowser::paintEvent(QPaintEvent*)':
CMerchandizeBrowser.cpp:481: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `virtual void CMerchandizeBrowser::resizeEvent(QResizeEvent*)':
CMerchandizeBrowser.cpp:486: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp: In member function `virtual void CMerchandizeBrowser::timerEvent(QTimerEvent*)':
CMerchandizeBrowser.cpp:492: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.cpp:493: error: invalid use of undefined type `struct CMerchandizeBrowserPrivate'
CMerchandizeBrowser.h:34: error: forward declaration of `struct CMerchandizeBrowserPrivate'
mingw32-make[1]: *** [debug/CMerchandizeBrowser.o] Error 1
mingw32-make[1]: Leaving directory `C:/Documents and Settings/markofr/workspace/eROSystem'
mingw32-make: *** [debug] Error 2but I assumed these errors are invoked because forward class declaration reports error.

jacek
6th January 2008, 18:43
It looks like you are missing #include directive in CMerchandizeBrowser.cpp. Where is CMerchandizeBrowserPrivate defined?

MarkoSan
6th January 2008, 18:58
CMerchandizeBrowserPrivate is defined in .cpp file. I think I will get some replies that defining class in .cpp file is not ok... :crying:

Thomas
6th January 2008, 19:45
Correct, you cannot define a class in a .cpp file.

jacek
6th January 2008, 19:57
CMerchandizeBrowserPrivate is defined in .cpp file.
Is the definition placed before the lines mentioned in error messages?


I think I will get some replies that defining class in .cpp file is not ok... :crying:
No, there's nothing wrong with it, but you won't be able to use that class outside that .cpp file.

Thomas
6th January 2008, 20:23
No, there's nothing wrong with it, but you won't be able to use that class outside that .cpp file.

I stand corrected. Yes you can. :o I was thinking of the accessability of those classes.

MarkoSan
6th January 2008, 21:45
Well, I've moved the class definition in .cpp file to the very beginning of the file, it compiles now!!!