2013-05-09 68 views
4

成員我有一個基類,稱爲GLObject,具有以下標題:{構造函數}不是的<BaseClass>

class GLObject{ 
    public: 
     GLObject(float width = 0.0, float height = 0.0, float depth = 0.0, 
       float xPos= 0.0, float yPos = 0.0, float zPos =0.0, 
       float xRot =0.0, float yRot = 0.0, float zRot = 0.0); 
    ... //Other methods etc 
}; 

而一個CPP:

GLObject::GLObject(float width, float height, float depth, 
        float xPos, float yPos, float zPos, 
        float xRot, float yRot, float zRot){ 
    this->xPos = xPos; 
    this->yPos = yPos; 
    this->zPos = zPos; 
    this->xRot = xRot; 
    this->yRot = yRot; 
    this->zRot = zRot; 
    this->width = width; 
    this->height = height; 
    this->depth = depth; 
} 

接下來我有一個派生類: 標題:

class GLOColPiramid : public GLObject 
{ 
public: 
    GLOColPiramid(float width, float height, float depth, float xPos = 0.0, float yPos = 0.0, float zPos = 0.0, float xRot = 0.0, float yRot = 0.0, float zRot = 0.0); 
    ... 
}; 

CPP文件

GLOColPiramid::GLOColPiramid(float width, float height, float depth, float xPos, float yPos, float zPos, float xRot, float yRot, float zRot) : GLObject::GLObject(width, height, depth, xPos,yPos,zPos,xRot,yRot,zRot) 
{ 

} 

這給了我一個錯誤:

glocolpiramid.cpp:4: error: C2039: '{ctor}' : is not a member of 'GLObject'

爲什麼呢?

我使用Qt 4.8.4與MSVC2010 32位編譯器

+0

嘗試刪除'GLObject ::''從:: GLObject在GLObject'聲明(CPP)。 – deepmax 2013-05-09 12:01:38

+0

哇,你說得對。這可能是真的,這不是Linux/g ++上的非法聲明? – Cheiron 2013-05-09 12:02:39

回答

5

嘗試從GLObject::GLObject在聲明中刪除GLObject::

.cpp文件,其中包含執行GLOColPiramid

GLOColPiramid::GLOColPiramid(....) : GLObject::GLObject(....) 
             ^^^^^^^^^^ 

這是一個在C++合法的,但測試它,也許MSVC2010有問題的。

+0

也許還應該提到您的意思是GLOColPiramid的cpp文件不是GLObject的文件。 – psibar 2013-05-09 12:21:14

1

從派生類構造函數調用它時,不應使用BaseClassName::BaseClassName(...)語法顯式引用基類構造函數 - 這正是編譯器所抱怨的。

相反,只使用基本的類名和傳遞的參數:

GLOColPiramid::GLOColPiramid(float width, float height, float depth, float xPos, float yPos, float zPos, float xRot, float yRot, float zRot) : GLObject(width, height, depth, xPos,yPos,zPos,xRot,yRot,zRot) 
{ 

}