我是 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!";
}
}
最佳答案
看起来你想做这样的事情:
t1rank += t1.get_player(i).get_rank();
在 C++ 中,方法调用的形式为 object.method(args)
。在您的情况下,您有两个方法调用,一个是 object
是 t1
并且方法是 get_player
,第二个是对象上一次调用的返回值,方法是get_rank
。
关于c++ - 函数 "was not declared in this scope",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10147223/