功能“未在此范围内声明”

时间:2012-04-13 19:35:51

标签: c++ compiler-errors

我是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!";
    }
}

1 个答案:

答案 0 :(得分:8)

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

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

在C ++中,方法调用的格式为object.method(args)。在您的情况下,您有两个方法调用,一个是objectt1,方法是get_player,第二个是对象是前一个调用的返回值,以及方法是get_rank