2013-11-02 66 views
0

我只想检查以便从父类继承构造函数,这是通过使用对象组合来完成它的正确方法吗?用于继承的C++构造函数

我得到这个错误,他们都指的是我的构造函数。

C:\Users\user\AppData\Local\Temp\cc0CYtVR.o:SquareImp.cpp:(.text+0x16): undefine 
d reference to `vtable for Square' 
C:\Users\user\AppData\Local\Temp\cc0CYtVR.o:SquareImp.cpp:(.text+0x79): undefine 
d reference to `vtable for Square' 
C:\Users\user\AppData\Local\Temp\cc0CYtVR.o:SquareImp.cpp:(.text$_ZN6SquareD1Ev[ 
Square::~Square()]+0xb): undefined reference to `vtable for Square' 
collect2: ld returned 1 exit status 

对不起,这是我的,因为我做了尝试重写他们

ShapeTwoD.h

class ShapeTwoD 
{ 
    protected: 
     string name, warpSpace; 
     bool containsWarpSpace; 
     int xCord, yCord, length, breath; 
    public: 
     //constructor 
     ShapeTwoD(); 
     ShapeTwoD(string, bool); 
}; 

ShapeTwoD.cpp

ShapeTwoD::ShapeTwoD() 
{ 
    string name = ""; 
    bool containsWarpSpace = true; 
} 

ShapeTwoD::ShapeTwoD(string ShapeName, bool warpspace) 
{ 
    name = ShapeName; 
    containsWarpSpace = true; 
} 

square.h

新的脚本
class Square:public ShapeTwoD 
{ 
    private: 
     int xVal,yVal; 
     int newlength, newbreath; 
     double area; 

    public: 
     Square(); 
     Square(string, bool, int, int); 
}; 

square.cpp

Square::Square() 
{ 
    xVal = 0; 
    yVal = 0; 
} 

    Square::Square(string ShapeName, bool warpspace, int xval, int yval):ShapeTwoD(ShapeName, warpspace), xVal(xval), yVal(yval) 
    { 
    xVal = xval; 
    YVal = yval; 
    cout << "test test" << endl; 
} 

int main() 
{ 
    Square square; 
    cout << "hello world" << endl; 
} 
+0

有什么你试过了吗?例如,您可以在构造函数中打印语句并确定它们的构建顺序。 – carlosdc

+0

看起来不错。在StackExchange网络中有一个[site](http://codereview.stackexchange.com/)用于代码审查,它可能更适合那里。 – nvoigt

+0

好吧,看来你已经编辑了你的问题。你如何宣布这些课程? – deepmax

回答

2

是的,也把xValyVal在初始化列表中太:

Square::Square(string ShapeName, bool square, int xval, int yval): 
     ShapeTwoD(ShapeName, square), xVal(xval), yVal(yval) 
{ 
} 

,构建基类Square()太:

Square::Square() : ShapeTwoD(..., ...) 
{ 
} 
+0

对不起,为什么我必须包含“xVal(xval),yVal(yval)”? –

+1

你不需要,你的方式可行,但这种方式是标准的,并且具有专门用于初始化对象的优点。 – deepmax