2010-06-20 113 views
2

我有從傳遞一個類中的一個回調函數,調用模板函數時麻煩函數模板。下面是一個示例代碼:類成員與調用回調函數

sortedlist.h

#ifndef _sortedlist_h 
#define _sortedlist_h 

#include <vector>  

template <typename ElemType> 
class SortedList { 
public: 
    SortedList(int (*compare)(ElemType a, ElemType b)); 
    ~SortedList(); 

    void push(ElemType newElem); 
    ElemType pop(); 

private: 
    std::vector<ElemType> v; 
    int (*cmp) (ElemType first, ElemType second); 
    void Sort(); 
}; 


template <typename ElemType> 
SortedList<ElemType>::SortedList(int (*compare)(ElemType a, ElemType b)) { 
    cmp = compare; 
} 

template <typename ElemType> 
SortedList<ElemType>::~SortedList() { 
} 

template <typename ElemType> 
void SortedList<ElemType>::push(ElemType newElem) { 
    v.push_back(newElem); 
    Sort(); 
} 

template <typename ElemType> 
ElemType SortedList<ElemType>::pop() { 
    ElemType next = v.back(); 
    v.pop_back(); 
    return next; 
} 

template <typename ElemType> 
void SortedList<ElemType>::Sort() { 
    for (int i=v.size()-1; i>0; i--) { 
     if(cmp(v[i], v[i-1]) < 0) { //compare function 
      ElemType temp = v[i]; 
      v[i] = v[i-1]; 
      v[i-1] = temp; 
     } 
     else return; 
    } 
} 

#endif 

game.h

#ifndef _game_h 
#define _game_h 

#include <string> 
#include "sortedlist.h" 

class Game { 
public: 
    Game() {}; 
    ~Game() {}; 
    void addPlayer(std::string name, int score); 
    std::string getWinner(); 

    struct Player { 
     std::string name; 
     int score; 
    }; 

    //compare function 
    int highScore(Player one, Player two); 

private:  
    SortedList<Player> list(highScore); 

}; 

#endif 

game.cpp

#include "game.h" 

void Game::addPlayer(std::string name, int score) { 
    Player newEntry; 
    newEntry.name = name; 
    newEntry.score = score; 
    list.push(newEntry);  
} 

std::string Game::getWinner() { 
    return list.pop().name; 
} 

//compare function 
int Game::highScore(Player one, Player two) { 
    if (one.score == two.score) return 0; 
    if (one.score > two.score) return 1; 
    return -1; 
} 

樣本主:

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

int main() { 
    Game pacman; 
    pacman.addPlayer("Beavis", 100); 
    pacman.addPlayer("Butthead", 200); 
    cout << pacman.getWinner() << endl; 
} 

當我在XCode上編譯它時,我得到「'高分'不是類型」錯誤。我也嘗試在類之外移動Player和highScore,得到相似的結果。我該怎麼做呢?

回答

4

在C++中,不能就地初始化類成員。你需要做的是在構造函數初始化列表

class Game { 
public: 
    Game():list(highScore) {}; 
    ~Game() {}; 

    //compare function 
    static int highScore(Player one, Player two); 

private:  
    SortedList<Player> list; 
}; 

功能需要被聲明爲類定義static作爲SortedList稱之爲無*this指針,像一個普通的功能。

表現確實比較函數不必要的不​​好,因爲你總是複製將它們作爲參數時要比較的兩個項目。更好地作出比較函數的類型接受常量引用和改變highScore的簽名適當

int (*compare)(ElemType const& a, ElemType const& b); 
+0

OK,我所做的所有的變化,但仍然獲得在構造以下錯誤:從 轉換無效「INT( *)(遊戲::播放機,遊戲::播放器)」到‘INT(*)(const的遊戲::玩家,常量遊戲::玩家)’ – Luciano 2010-06-20 18:28:10

+0

@Luciano:那建議你忘了做出改變的地方( 'highScore'函數沒有引用?)。在C++中,使用'std :: sort'或'std :: stable_sort'可能會更常見,並使用決定「小於」關係的謂詞('bool compare_scores(const Player&a,const Player&b){返回a.score UncleBens 2010-06-20 19:17:59

+0

是的,你是對的。我忘了將highScore更改爲const&。 我可以使用std :: sort,但是賦值的全部要點是創建回調函數。謝謝! – Luciano 2010-06-20 19:34:08