2010-05-29 146 views
1

试图找出如何使用具有继承类的构造函数。我知道这是非常错误的,我一直在写C++约三天了,但这里是我的代码吗:C++继承和构造函数

clientData.h,两班,ClientData扩展实体:

#pragma once 

class Entity 
{ 
public: 
int x, y, width, height, leftX, rightX, topY, bottomY; 

Entity(int x, int y, int width, int height); 
~Entity(); 
}; 

class ClientData : public Entity 
{ 
public: 
ClientData(); 
~ClientData(); 
}; 

和clientData的.cpp,其中所包含的功能:

#include <iostream> 
#include "clientData.h" 
using namespace std; 

Entity::Entity(int x, int y, int width, int height) 
{ 
this->x = x; 
this->y = y; 
this->width = width; 
this->height = height; 

this->leftX = x - (width/2); 
this->rightX = x + (width/2); 
    this->topY = y - (height/2); 
this->bottomY = y + (height/2); 
} 

Entity::~Entity() 
{ 
cout << "Destructing.\n"; 
} 

ClientData::ClientData() 
{ 
cout << "Client constructed."; 
} 

ClientData::~ClientData() 
{ 
    cout << "Destructing.\n"; 
} 

最后,我创建一个新的ClientData有:

ClientData * Data = new ClientData(32,32,32,16); 

现在,我并不惊讶我的编译器在我身上发出了错误,所以我如何将这些参数传递给正确的类?

的第一个错误(从MVC2008)是 错误C2661:“ClientData :: ClientData”:没有重载函数有4个参数

;第二,它会弹出我似乎什么变化,使是 错误C2512 :'实体':没有适当的默认构造函数可用 谢谢。

+3

你可能想说出你的编译器在你身上发出的什么错误! – LukeN 2010-05-29 13:34:31

+2

阅读C++入门。 – starblue 2010-05-29 13:34:35

回答

4

目前的构造器客户端数据类不会工作。您将需要一个构造像客户端数据:

ClientData(int x, int y, int width, int height): Entity(x, y, width, height) 

,如果你想打电话

new ClientData(32,32,32,16); 
+0

无效的语法。 – kennytm 2010-05-29 13:41:54

+2

这是完全有效的语法。这是解决这个问题的唯一方法...... – SoapBox 2010-05-29 13:43:51

+0

@Soap:当我编写该评论时,语法无效。它是在5分钟内编辑的,现在很好。 – kennytm 2010-05-29 15:52:09

0

你可以使用初始化列表来调用基类的构造函数(注:也可用于调用该对象类对象的构造函数):

class Base 
{ 
    private: 
    int myVal; 

    public: 
    Base(int val) 
    { 
     myVal = val; 
    } 
}; 

class Heir : public Base 
{ 
    private: 
    std::string myName; 

    public: 
    Heir(std::string Name, int Val) : Base(Val) 
    { 
     myName = Name; 
    } 
}; 
2

使用constructor initializer初始化基地和成员:

struct Entity { 
    int x, y, width, height, leftX, rightX, topY, bottomY; 

    Entity(int x, int y, int width, int height); 
}; 

Entity::Entity(int x, int y, int width, int height) 
: x(x), y(y), width(width), height(height), 
    leftX(x - (width/2)), rightX(x + (width/2)), 
    topY(y - (height/2)) 
{ 
    bottomY = y + (height/2); // for members like leftX, rightX, topY, 
    // and bottomY, assignment inside the ctor (instead of initialization) 
    // can be appropriate 
} 


struct ClientData : Entity { 
    ClientData(); 
    ClientData(int x, int y, int width, int height); 
}; 

ClientData::ClientData() : Entity(0, 0, 0, 0) {} // you may not even want a 
// default ctor for this type 

ClientData(int x, int y, int width, int height) 
: Entity(x, y, width, height) 
{} 
3

第一点

new ClientData(32,32,32,16); 

将无法​​工作,因为您对ClientData的唯一构造函数不带任何参数。构造函数不是用C++继承的,你必须再次定义构造函数。

class ClientData : Entity 
{ 
    public: 
    ClientData(int a,int b,int c,int d); 
    //... 
} 

其次是调用基类的构造函数。通常情况下,编译器使用基类的非参数构造函数调用,因为实体只有一个构造函数参数会失败 - 您必须对实体构造函数进行显式调用。

ClientData::ClientData(int a,int b, int c, int d) 
: Entity(a,b,c,d)//Initializer list call base class constructor here 
{ 
//... 
} 
0

我对代码做了一些更改。它可以帮助你了解C++类,成员和构造函数。

当使用继承的类,则必须调用基类的构造与派生类的构造所需的参数:

ClientData :: ClientData(INT的x,INT Y,INT宽度,INT高度):实体(X, y,width,height)

只是一个观点:不要使用与类成员相同的参数名称。我通常在班级成员之前使用'm_'前缀以便于识别。

class Entity 
{ 
public: 
int m_x, m_y, m_width, m_height, m_leftX, m_rightX, m_topY, m_bottomY; 

Entity(int x, int y, int width, int height); 
~Entity(); 
}; 

class ClientData : public Entity 
{ 
public: 
ClientData(int x, int y, int width, int height); 
~ClientData(); 
}; 

Entity::Entity(int x, int y, int width, int height) 
{ 
m_x = x; 
m_y = y; 
m_width = width; 
m_height = height; 

m_leftX = x - (width/2); 
m_rightX = x + (width/2); 
m_topY = y - (height/2); 
m_bottomY = y + (height/2); 
} 

ClientData::ClientData(int x, int y, int width, int height) : Entity(x, y, width, height) 
{ 
cout << "Client constructed."; 
}