首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:“object”未在此作用域中声明

错误:“object”未在此作用域中声明
EN

Stack Overflow用户
提问于 2017-01-08 14:33:01
回答 1查看 7.5K关注 0票数 4

我在c++是个新手,正在努力制造垄断游戏。不幸的是,它仍然在两个类之间的声明中显示了错误。我已经什么都试过了,真的不知道问题出在哪里。

错误:“Player”不在此范围内声明。

Engine.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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() {
}

Player.h

代码语言:javascript
复制
#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 */

Player.cpp

代码语言:javascript
复制
#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() {
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-08 14:41:09

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

您应该:

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

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41533808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档