2016-11-26 112 views
-4

我需要在main()我如何分配類對象爲類

是分配給它的進級PokemonWorld以創造一個對象寵物小精靈,讓PokemonWolrd決定哪些PokemonStation這是口袋妖怪需要去

我累了獲取數據separatly(取姓名和馬力)和聚(獲得寵物小精靈類)

但都失敗

#include <iostream> 
#include <cstring> 
#include <iomanip> 

using namespace std; 



class Pokemon { 
public: 
    Pokemon() {}; 

    Pokemon(char x[], int n) { 
     strncpy_s(name, x, 10); 
     hp = n; 
    }; 
private: 
    char name[10]; 
    int hp; 
}; 

class PokemonStation { 
private: 
    Pokemon **list= new Pokemon*[1000]; 
public: 
    PokemonStation() {}; 
    PokemonStation(int x) { 
     id = x; 
    }; 
    int id; 
    void assigntoList(int i,Pokemon x) 
    { 
     if (i > 0) 
      i--; 
     list[i] = new Pokemon(x); 
     cout << "creat" << list[i]; 
    }; 
}; 

class PokemonWorld { 
private: 
    char name[10]; 
public: 
    PokemonStation s1; 
    PokemonStation s2; 
    PokemonWorld() {}; 
    PokemonWorld(char x[], int y=1, int z=2) { 
     strncpy_s(name, x, 10); 
     PokemonStation s1(y); 
     PokemonStation s2(z); 

    }; 
    const char* const getName() const{ 
     return name; 
    }; 
    void assigntoStation(int i,Pokemon x) { 
     if (i == 0 || i % 2 == 0) 
      s1.assigntoList(i, x); 
     else 
      s2.assigntoList(i, x); 
    }; 
}; 

void main() { 
    int number,hp,i; 
    char name[10]; 
    cout << "What is the World Name ?" <<endl; 
    cin >> name; 

    PokemonWorld world(name); 

    cout << "Please input the number of Pokemon in the " << world.getName() <<" world:" << endl; 
    cin >> number; 
    Pokemon **mon = new Pokemon*[number]; 
    cout << "Please input the characteristics of all Pokemon: Name HP" << endl; 
    for (i = 0;i < number;i++) 
    { 
     cin >> name >> hp; 
     mon[i] = new Pokemon(name, hp); 
     world.assigntoStation(i,*(mon[i])); 
    } 
    for (i = 0;i < number;i++) 

    cout << "world is " << world.getName() << endl; 


    system("pause"); 

}; 
+0

如果你想在C++中使用動態列表,你應該使用['std :: vector'](http://www.cplusplus.com/reference/vector/vector/)而不是原始數組。也可以使用['std :: string's](http://www.cplusplus.com/reference/string/string/)作爲文本。 – qxz

+0

你的程序究竟發生了什麼不工作? – qxz

+0

當我嘗試打印出PokemonStation中的函數assigntoList中的小寵物來測試我是否成功地將口袋妖怪分配到列表[i]中時,它打印出列表[i] –

回答

0

在C++中,你應該ū se std::vectors爲動態列表的東西和std::strings爲文本。如果你知道Java,這些就像ArrayListString。 (要使用這些,請確保您#include<vector><string>。)

比如你Pokemon類,與name改寫爲string

class Pokemon { 
public: 
    Pokemon() {} 

    Pokemon(string name, int hp):name(name), hp(hp) { // construct the fields directly 

    } 
private: 
    string name; 
    int hp; 
}; 

和你PokemonStation類,使用了一個vector改寫口袋妖怪的名單:

class PokemonStation { 
private: 
    vector<Pokemon> list; 
public: 
    PokemonStation() {} 
    PokemonStation(int x):id(x) {} 
    int id; 
    void assignToList(Pokemon x) 
    { 
     list.push_back(x); // add x to the list 
    } 
}; 

如果你要打印Pokemoncout << ,那麼你必須重載<<運算符來定義打印的內容。這種添加到您的類:

class Pokemon { 
public: 
    ... 
    friend ostream& operator<<(ostream& out, const Pokemon& p) { 
     out << p.name; // just print the name 
     return out; 
    } 
    ... 
}; 

只要確保你cout荷蘭國際集團一Pokemon,而不是一個指針TO- PokemonPokemon*),你不會得到一個地址。

相關問題