2015-05-28 25 views
0

的構造函數創建一個對象由於某種原因,每當我嘗試運行我的代碼時,它總是調用默認構造函數,但它應該使用參數調用構造函數。如何用參數

#include "pokemon.h" 

int main() 
{ 
    int choice; 

    cout<<"input 1 2 or 3"<<endl; 
    cin>>choice; 

    if(choice==1||choice==2||choice==3) 
    { 
     pokemon(choice); 
    } 

} 
我headerfile

我有

#include <iostream> 
#include <string> 
#include <stdlib.h> 
#include <time.h> 

using namespace std; 

class pokemon{ 
public: 
    pokemon();//default constructor 
    pokemon(int a); 
    ~pokemon();//desconstructor 
    pokemon(const pokemon& c); 
    void train(); 
    void feed(); 
    bool isnothappy(); 
    string getName();//accessor for the name 
    int getPowerlevel();//accessor for the power level 
    string getColor();//accessor for the color 
    string getType();//accessor 
    int getHappylevel();//accessor 
    static int getNumObjects(); 
    void set_type(string);//mutator 
    void set_color(string);//mutator 
    void set_power_level(int);//mutator 
    void set_happy_level(int);//mutator 
    void set_name(string);//mutator 
private: 
    string name; 
    string color; 
    string type; 
    int power_level; 
    int happy_level; 
    static int numberobject; 
}; 

和我在其他.cpp文件我有

int pokemon::numberobject=0;//initialize static member variable 

pokemon::pokemon(){//default constructor 
    name="pikachu"; 
    color="yellow"; 
    type="electric"; 
    power_level=0; 
    happy_level=1; 

    cout<<"The default constructor is being called"<<endl; 
    ++numberobject; 
} 

pokemon::pokemon(int a) 
{ 

    if(a==0) 
    { 
     name="Pikachu"; 
     color="yellow"; 
     type="electric"; 
     power_level=1; 
     happy_level=1; 
    } 


    else if(a==1) 
    { 
     name="Bulbasaur"; 
     color="green"; 
     type="grass"; 
     power_level=1; 
     happy_level=1; 
    } 


    else if(a==2) 
    { 
     name="Charmander"; 
     color="red"; 
     type="fire"; 
     power_level=1; 
     happy_level=1; 
    } 

    else if(a==3) 
    { 
     name="Squritle"; 
     color="blue"; 
     type="water"; 
     power_level=1; 
     happy_level=1; 
    } 

    cout<<"Congratulations you have chosen "<<getName()<<". This " <<getColor()<<" "<<getType()<<" pokemon is really quite energetic!"<<endl; 
    ++numberobject; 
} 

pokemon::~pokemon() 
{ 
    //cout<<"the destructor is now being called"<<endl; 
    //cout<<"the number of objects before the destructor is "<<pokemon::getNumObjects()<<endl; 
    --numberobject; 
    cout<<"Now you have a total number of "<<pokemon::getNumObjects()<<endl; 
} 

pokemon::pokemon(const pokemon& c)//copy constructor 
{ 
    name=c.name; 
    color=c.color; 
    type=c.type; 
    power_level=c.power_level; 
    happy_level=c.happy_level; 
    ++numberobject; 
} 

我有我的兩個構造函數聲明,我在其他文件中定義的,但這該死的東西始終稱默認構造函數

+0

'pokemon(choice);'這條線做什麼? – PaulMcKenzie

+0

@PaulMcKenzie口袋妖怪(選擇)甚至沒有得到任何理由,因爲它只是去默認的構造函數得到執行 – Brogrammer93

+0

你能告訴我們一個'pokemon.h'的片段 – dlavila

回答

3

此代碼:

pokemon(choice); 

意思是一樣的:

pokemon choice; 

它聲明瞭一個名爲pokemonchoice變量,並且沒有給構造函數的參數。 (您可以在某些地方的聲明中添加額外的括號)。


如果你的意思是聲明一個變量,其中choice是一個構造函數的參數,那麼你必須寫:

pokemon foo(choice); 

如果您打算創建一個臨時對象(將被立即銷燬)與choice作爲參數,您可以編寫(pokemon)choice;pokemon(+choice);,或自C++ 11,pokemon{choice};


與聲明和非聲明之間的模糊這個問題可能出現的任何時間聲明始於類型名後跟(。規則是,如果它在語句上對於聲明是正確的,那麼它將被視爲聲明。其他類似情況見