c++ - 函数 "was not declared in this scope"

标签 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!";
    }
}

最佳答案

看起来你想做这样的事情:

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

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

关于c++ - 函数 "was not declared in this scope",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10147223/

相关文章:

linker - Lua编译链接错误

compiler-errors - gfortran : compile source codes from hierarchically dependent subroutine files

haskell - 为什么我的谓词被解释为 bool 值? ( haskell )

vb.net - Visual Basic中的语句结尾预期错误

c++ - 树中两个节点之间的最小距离

c++ - 重载 << 运算符和递归

c++ - 关于C++模板特化和部分模板特化的疑问

Python:为什么我在 MacOSX 上导入 glpk 时会遇到此错误?

c++ - 为什么信号量的条件/互斥实现在其 "while"函数中需要一个 "wait()"循环?

c++ - 分配类 C++ 的数组