【问题标题】:error: 'object' was not declared in this scope错误:未在此范围内声明“对象”
【发布时间】:2017-01-08 14:33:01
【问题描述】:

我是 C++ 的新手,正在尝试制作 Monopoly 游戏。不幸的是,它仍然显示两个类之间的声明错误。 我已经尝试了所有方法,真的不知道问题出在哪里。

错误:“播放器”未在此范围内声明。

引擎.h

#ifndef ENGINE_H
#define ENGINE_H
#include "Player.h"
#include <vector>
using namespace std;
class Engine{
public:
    Engine(); // method that starts with game, take random number for getting number of players, set players to vector
    void play(); // method where players are playing.
    bool returnBalance(int a_money) const; // method that return True, if the players has still some amount on account, False otherwise
    bool isWinner();
    int setBalance(); // method that set curretn player amount
    void printWinner(); // method that print winter of the game
    void payBills(int amount); // player pay any bills with this method
    virtual ~Engine();
private:
    vector<Player*> players;
    int i_player;
    int balance;
    int currentPlayer;
};
#endif /* ENGINE_H */

Engine.cpp

#include "Engine.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

Engine::Engine() {
    int numPlayers = rand()*(6-2)+2;
    for (int i = 0; i <= numPlayers; i++){
        Player* p = new Player;
        players.push_back(p);
    }
     cout << players.size() << endl; 

    int p_index = 0; 
     for(int i = 1; i <= players.size(); i++){
         p_index = i;
         p_index++;
         cout << p_index ;
     }
    currentPlayer = p_index;     

    cout << "Welcome to MonOOpoly game, the game will be played in the same order you already are." << endl;
}

void Engine::play() {
    do{

    }while(!isWinner());
}

bool Engine::isWinner(){
    int count = 0;
    for(int i = 1; i <= players.size(); i++){
        if(players[i]->getAmount() > 0)
            count++; 
    }
    if(count <= 1)
        return true;
    return false;

}

int Engine::setBalance(){
    int amount = players[currentPlayer]->amount;
    return players[currentPlayer]->amount;
}

bool Engine::returnBalance(int a_money) const{
    if (players[currentPlayer]->amount < a_money)
        return false;
    else 
        return true;
}

void Engine::payBills(int amount) {
    players[currentPlayer]->amount = players[currentPlayer]->amount - amount;
}

void Engine::printWinner() {
    int winner = 0;
    int newWinner = 0;
    for(int i = 1; i <= players.size(); i++){
        if(players[i] > 0){
            winner = players[i]->getAmount();
            if(newWinner < winner)
                newWinner = winner;
        }
    }
    cout << "Winner of the game MonOOpoly is: " << newWinner << endl;
}

Engine::~Engine() {
}

播放器.h

#ifndef PLAYER_H
#define PLAYER_H
#include "Engine.h"
#include <string>
using namespace std;

class Player {
    friend class Engine;
public:
    Player(); // constructor
    int getAmount() const; // return how much of amount the player has yet
    void setAmount(int a); // set amount
    int getPosition() const; // return position of the player
    void setPosition(int p); // to set position
    virtual ~Player(); // destructor
private:
    int position; // the position of the player
    int amount; // the total amount
};


#endif /* PLAYER_H */

播放器.cpp

#include <iostream>
#include <stdlib.h>
#include "Player.h"
using namespace std;

Player::Player() {
    amount = 5000;
    position = 0;
}

int Player::getAmount() const {
    return amount;
}

void Player::setAmount(int a) {
    amount = a;
}

int Player::getPosition() const {
    return position;
}

void Player::setPosition(int p) {
    position = p;
}

Player::~Player() {
}

【问题讨论】:

  • 尝试使用编译指示一次或类似的东西。由于 Engine.h 包含 Player.h,而 Player.h 包含 Engine.h,因此这里可能会发生一些冲突。
  • @eranotzap:他有非常好的包含守卫,不依赖于实现相关的行为。问题是当他包含 player.h 时,它包含 engine.h,它试图包含 player.h - 但实际上什么也没做。在那个指针处,engine.h 尝试使用类播放器 - 但它没有被声明。
  • Unrelated :您应该将players 向量设为std::unique_ptr&lt;Player&gt; 的向量。这样,当向量被销毁时,所有对象都会被删除。目前,您正在泄露所有内容。
  • @MartinBonner 首先没有注意到保护守卫。我是 C++ 新手,没有遇到您描述的问题。我们可以再看一遍吗? player.h -> engine.h -> 尝试 player.h 但由于保护措施不包括在内,并且不知道任何 Player 类。那么她如何在 Engine 中使用 Player 呢?

标签: c++ class declaration


【解决方案1】:

您的标头中有循环包含,可能会导致您看到的编译器问题。 Engine.h 包含 Player.h,Player.h 包含 Engine.h

你应该:

  • #include Player.h 从 Engine.h 移动到 Engine.cpp
  • 在 Engine.h(带有class Player;)行中前向声明 Player 类。

前向声明在这里很有用,因为Engine.h 只需要知道某个 Player 类的存在而不是它的完整定义,因为它只是定义了指向该类的简单指针向量。

【讨论】:

  • 您能否展示一下您将如何做到这一点。我没有得到第二点。她怎么会感染引擎中的使用玩家?
  • 第二点是建议在 Engine.h 中用class Player.h 替换行#include "Player.h"
  • @eranotzap:如果您说“class Player;”,则表示声明该类,但没有定义它。一旦你声明了一个类,你就可以使用指向该类的指针和引用——但你不能拥有该类的实际对象。类 Engine 只包含指针,所以没问题。 (很明显,Engine 的成员函数将需要完整的 Player 定义,这就是您将其包含在 .cpp 中的原因)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
相关资源
最近更新 更多