2

我是 C++ 新手,我正在尝试编写一个模拟足球比赛的程序。我收到一个编译器错误,指出函数 get_rank、get_player 和 get_name 未在此范围内声明。任何帮助是极大的赞赏!

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Player {
    int playerNum;
    string playerPos;
    float playerRank;
    public:
        void set_values(int, string, float);
        float get_rank(){ return playerRank; };
};

class Team {
    Player team[];
    string teamName;
    public:
        void set_values(Player[],string);
        Player get_player(int a) { return team[a]; };
        string get_name() { return teamName; };
};


void play(Team t1, Team t2){
    float t1rank = 0.0;
    float t2rank = 0.0;
    for(int i=0; i<11; i++){
        t1rank += get_rank(get_player(t1, i));
    }
    for(int j=0; j<11; j++){
        t2rank += get_rank(get_player(t2, j));
    }
    if(t1rank>t2rank){
        cout << get_name(t1) + " wins!";
    }
    else if(t2rank>t1rank){
        cout << get_name(t2) + " wins!";
    }
    else{
        cout << "It was a tie!";
    }
}
4

1 回答 1

8

看起来你想做类似的事情:

    t1rank += t1.get_player(i).get_rank();

在 C++ 中,方法调用的形式为object.method(args). 在您的情况下,您有两个方法调用,一个 where objectist1和 method is get_player,第二个 object 是前一个调用的返回值,并且方法是get_rank.

于 2012-04-13T19:37:13.427 回答