2010-06-20 102 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