2017-05-04 126 views
0

這些是頭文件我的類函數:類函數的指針

public: 
    hunter(string aSpecies);   // create a hunter of the given species 
    void recordKills(string kill); // add a new kill to the end of the hunter's list of kills 
    string *theKills();    // return a pointer to the array of all kills by this hunter 
    int numberOfKills();    // how many kills have been recorded 

和類變量:

private: 
    string kill; 
    int numberkilled; 
    string kills[20]; 

我不知道該如何處理「串* theKills()」

我試圖做這樣:

string hunter::*theKills(){ 
    pointer = kills; 
    return pointer; 
} 

與*不能識別kills作爲我的類變量的一部分,但我們應該使用相同的函數名稱。

+2

不應該是'string * hunter :: theKills(){'? – songyuanyao

回答

0

的語法去如下:

<return type> <class name>::<function name>(<parameters>); 

而你的情況是:

  • <return type>string *
  • <class name>hunter
  • <function name>theKills
  • <parameters>:無
string * hunter::theKills() { 
    return kills; // you don't need a temporary pointer variable 
} 

保存你使用指針的麻煩,我建議你使用一個std::array,而不是你的C數組string kills[20]

std::array<std::string, 20> kills; 

記住添加const預選賽到每個沒有修改你班級任何成員的成員功能。

我猜這是bad practice的使用。

+0

std :: array和C數組有什麼區別。 也感謝您的幫助您的冠軍語法 – Darko

+0

@Darko看看[這裏](https://isocpp.org/wiki/faq/containers)。 –

+0

感謝堆隊友,我知道它似乎很小,但你的幫助是非常感激! – Darko