草庐IT

c++ - 错误 : 'object' was not declared in this scope

coder 2024-02-20 原文

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

错误:“玩家”未在此范围内声明。

引擎.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 */

引擎.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

你应该:

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

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

关于c++ - 错误 : 'object' was not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41533808/

有关c++ - 错误 : 'object' was not declared in this scope的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  3. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  4. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  5. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  6. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  7. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  8. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  9. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  10. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

随机推荐