2013-05-09 70 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) 
{ 

}