C奇怪的编译错误:错误:从类“对象”更改“对象”的含义

新手上路,请多包涵

我什至不知道该去哪里。谷歌不是很有帮助。和我之前的问题一样。我正在使用 TextMate 的 Command+R 来编译项目。

game.h:16:error: ‘Player* HalfSet::Player() const’ 的声明

player.h:11:error: 将“玩家”的含义从“类玩家”更改

game.h:21:error: ‘Player’ 不是类型

player.h 文件(部分)

 #ifndef PLAYERS_H
#define PLAYERS_H
using namespace std;

#include <string>
#include <vector>
#include <istream>
#include <iomanip>
#include "generics.h"

class Player{ //Line 11
public:
    //getters
    long Id() const;
    string FirstName() const;
    string LastName() const;
    string Country() const;
    //setters
    void setId(long id);
    void setFirstName(string s);
    void setLastName(string s);
    void setCountry(string s);
    //serializing functions
    void display(ostream &out);
    void read(istream &in);
    void write(ostream &out);
    //Initalizers
    Player();
    Player(istream &in);
    Player(string firstName, string lastName);
    Player(string firstName, string lastName, string country);
    Player(long id, string firstName, string lastName, string country);
    ~Player();
private:
    long _id;
    string _firstName;
    string _lastName;
    string _country;
};

game.h 文件(部分)

 #ifndef GAME_H
#define GAME_H

#include "generics.h"
#include "players.h"
#include <string>
#include <vector>
#include <istream>
#include <iomanip>

using namespace std;

class HalfSet{
public:
    //getters
    Player* Player() const; //Line 16
    int GamesWon() const;
    int TotalPoints() const;
    int Errors() const;
    //setters
    void setPlayer(Player* p);
    void setGamesWon(int games);
    void setTotalPoints(int points);
    void setErrors(int errors);
    //Serialization
    void display(ostream &out) const;
    void read(istream &in) const;
    void write(ostream &out) const;
    //Initalizers
    HalfSet();
    ~HalfSet();
private:
    Player* _player;
    int _gamesWon;
    int _points;
    int _errors;
};

这里发生了什么?

原文由 epochwolf 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 191
1 个回答

在 C++ 中,您不能将函数命名为与类/结构/类型定义相同的名称。您有一个名为“Player”的类,因此 HalfSet 类有一个名为“Player”的函数(“Player *Player()”)。您需要重命名其中之一(可能将 HalfSet 的 Player() 更改为 getPlayer() 或类似名称)。

原文由 SoapBox 发布,翻译遵循 CC BY-SA 2.5 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏