2010-05-29 143 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."; 
}