小编pig*_*d10的帖子

C++ for循环和switch语句逻辑错误

在此循环中(假设amount为2),而不是打印:

1. [op1]
2. [op2]
Run Code Online (Sandbox Code Playgroud)

它打印:

1. [op1]
2. [op2]
3. [op3]
4. [op4]
5. [op5]
Run Code Online (Sandbox Code Playgroud)

为什么?我怎样才能解决这个问题?

for(int i=1;i<=amount;i++){
            //string s;
            switch(i){
            case 1:
                cout << i << ". " << op1 << endl;
                //s = op1;
            case 2:
                cout << i << ". " << op2 << endl;
                //s = op2;
            case 3:
                cout << i << ". " << op3 << endl;
                //s = op3;
            case 4:
                cout << i << ". " << op4 << …
Run Code Online (Sandbox Code Playgroud)

c++ loops for-loop switch-statement

1
推荐指数
1
解决办法
532
查看次数

C++ - 使用atoi时的未处理异常()

使用此代码时,它会抛出一个未处理的写入异常,我几乎可以肯定这与atoi()函数有关.

while(true){
                    char* item = "";
                    cin >> item;
                    int numItem = atoi(item);
                    if(numItem){
                        if(numItem<=backpackSpaces){
                                equipItem(backpack[numItem]);
                                break;
                        }else{
                            cout << "No such item." << endl;
                        }
                    }else if(item == "back"){
                        cout << "Choose an option from the original choices. If you can't remember what they were, scroll up." << endl;
                        break;
                    }else{
                        cout << "Command not recognised." << endl;
                    }
}
Run Code Online (Sandbox Code Playgroud)

c++ unhandled exception atoi

0
推荐指数
1
解决办法
1099
查看次数

字符串连接与整数

Damage并且Cost是整数,但正如您在下面的代码中看到的,我想用字符串连接它们(如果这是正确的单词).我怎样才能做到这一点?

class Weapon : Shopable{
    private:
        int Damage;
    public:
        std::string getDesc() const{
            return getName()+"\t"+Damage+"\t"+Cost;
        }
};
Run Code Online (Sandbox Code Playgroud)

c++ string integer concatenation

0
推荐指数
1
解决办法
1151
查看次数

与凌乱的OOP代码有关的大量错误

我得到的负载错误的这个时候我编译.如果我解决其中一个问题,希望事情会变得更加清晰(如果没有,我可以发布其余部分):

'Weapon' : illegal member initialization: 'Name' is not a base or member

它具有名称和成本.武器继承Shopable,Shopable在其受保护的部分中具有名称,成本和描述.

Shopable.h:

#ifndef _SHOPABLE_H_
#define _SHOPABLE_H_

#include "Library.h"

class Shopable{
protected:
    std::string Name;
    int Cost;
    std::string Description;
public:
    std::string getName() const{return Name;}
    int getCost() const {return Cost;}
    virtual std::string getDesc() const = 0;
};

#endif
Run Code Online (Sandbox Code Playgroud)

Weapon.h:

#ifndef _WEAPON_H_
#define _WEAPON_H_

#include "Shopable.h"

class Weapon : public Shopable{
private:
    int Damage;
public:
    Weapon(int c,int d,std::string n) : Cost(c), Damage(d), Name(n){}
    std::string getDesc() const{
        return getName()+"\t"+tostring(Damage)+"\t"+tostring(Cost);
    } …
Run Code Online (Sandbox Code Playgroud)

c++ oop

0
推荐指数
1
解决办法
158
查看次数

作为参数的类错误

在Weapon.h中,当我尝试将类'Entity*'作为参数时,它在编译时会显示"语法错误:标识符'实体'".另外,当我滚动文本'target'时,Visual C++ Express 2010为我提供了文本"*target".Entity类很好,我很确定它包含正确.

(我不会发布Player.h,因为它是不必要的 - 请参阅Library.h - 但它有一个标题保护并包含Entity.h)

Library.h:

#ifndef _LIBRARY_
#define _LIBRARY_

#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdarg>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <map>
#include <exception>
#include <sstream>

//file includes
#include "Globals.h"
#include "Player.h"
#include "Exception.h"
#include "Weapon.h"
#include "Armour.h"
#include "Consumable.h"

//prototypes that require "Library.h"
bool Poglathon(std::vector<std::string>& text,Player *player);
bool PoglathonTown(std::vector<std::string>& text,Player *player);

std::map<std::string,Weapon*> init_weapons(void);
std::map<std::string,Armour*> init_armour(void);
std::map<std::string,Consumable*> init_consumables(void);

#endif //__LIBRARY__
Run Code Online (Sandbox Code Playgroud)

Weapon.h:

#ifndef _WEAPON_H_
#define _WEAPON_H_

#include …
Run Code Online (Sandbox Code Playgroud)

c++ oop parameters class function

0
推荐指数
1
解决办法
141
查看次数

map/set erase iterator超出范围

当我在特定状态下关闭我的应用程序时,此代码运行:

void GameEndState::exit(){
    delete m_pGameObjectManager;
    delete m_pSceneManager;
}
Run Code Online (Sandbox Code Playgroud)

运行m_pGameObjectManager的析构函数后,它是:

GameObjectManager::~GameObjectManager(){
    std::for_each(gameObjects.begin(),gameObjects.end(),GameObjectDeallocator());
}
//gameObjects is a std::map<sf::String,VisibleGameObject*>

///////////////

struct GameObjectDeallocator{
        void operator()(const std::pair<sf::String,VisibleGameObject*>&p) const{
            delete p.second;
        }
    };
Run Code Online (Sandbox Code Playgroud)

它会产生标题的错误.我没有从谷歌上找到任何这个错误的结果.我正在使用SFML 2.0.

VisibleGameObject的析构函数:

VisibleGameObject::~VisibleGameObject(){
    m_pSceneManager->removeSprite(name);
}

void SceneManager::removeSprite(sf::String spritename){
    std::cout << "removed " << std::string(spritename) << std::endl;
    sprites.erase(sprites.find(spritename));
}

//sprites is a std::map<sf::String,sf::Sprite>
Run Code Online (Sandbox Code Playgroud)

提前致谢

c++ map sfml

0
推荐指数
1
解决办法
1839
查看次数