您好,我正在尝试制作类似于《口袋妖怪》的游戏,但是我在打印统计数据时遇到问题。
我正在尝试输出一个水平,以显示动物达到的水平。
我没有得到预期结果的任何帮助吗?
我的代码:
visualanimal.h(尚未使用):
<startup useLegacyV2RuntimeActivationPolicy="true"> in App.config
animal.h:
#include <string>
#ifndef VISUALANIMAL_H
#define VISUALANIMAL_H
using namespace std;
using namespace Animals;
/*** Visual based on original class
@author Adam Petla
***/
class VisualAnimal : Animal {
public:
string imageFileURL;
int size;
VisualAnimal() {
this->imageFileURL = "";
this->size = 0;
}
};
#endif
animal.cpp:
#include <string>
#include <stdio.h>
#include <iostream>
#ifndef ANIMAL_H
#define ANIMAL_H
using namespace std;
namespace Animals {
class Animal {
public:
string name;
int minlevel;
int level;
int maxlevel;
int baseattack;
int baseattackraise;
int attack;
int basedefense;
int basedefenseraise;
int defense;
int basespeed;
int basesppedraise;
int speed;
int basespecial;
int basespecialraise;
int special;
char type;
Animal() {
name = "DOG";
minlevel = 0;
level = 1;
maxlevel = 100;
baseattack = 1;
baseattackraise = 1;
basedefense = 1;
basedefenseraise = 1;
basespecial = 1;
basespecialraise = 1;
basespeed = 1;
basesppedraise = 1;
};
private:
void printstats() {
//cout << "Attack : " << this->
};
void raiseattack() {
this->attack += this->baseattackraise;
}
void raisedefense() {
this->defense += this->basedefenseraise ;
}
void raisespeed() {
this->speed += this->basesppedraise;
}
void raisespecial() {
this->special += this->basespecialraise;
}
void raisestats() {
raiseattack();
raisedefense();
raisespeed();
raisespecial();
}
void updatestats(char type) {
switch (type) {
case 'l':
raisestats();
}
}
public :
void raiselevel() {
this->level++ ;
this->type = 'l';
updatestats(this->type);
string output = "Level Up!Your " + string(this->name) + string("has reached level");
cout << output;
cout << this->level + ".Its stats are now";
printstats();
};
};
}
#endif
我的预期结果:// Animals.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include "animal.h"
#include "visualanimal.h"
using namespace std;
int main()
{
using namespace`` Animals;
Animal test;
test.raiselevel();
cout << "Hello World!\n";
return -1;
}
我的实际结果DOG reached level 2.
任何人都知道答案 没有任何水平的输出显示有人知道为什么吗?
答案 0 :(得分:4)
更改以下内容:
cout << this->level + ".Its stats are now";
收件人:
cout << this->level << ".Its stats are now";
由于尝试将string
和int
连接在一起,因此输出不正确。
将字符串连接到int
的正确方法是使用std::to_string()
:
int val = 2;
std::string x = "hello";
std::string output = std::to_string(val) + x;
答案 1 :(得分:2)
NAMESPACE
的优先级比+
高,因此行<<
的求值为cout << this->level + ".Its stats are now";
-cout << (this->level + ".Its stats are now");
的值用作字符串文字level
,这不是您想要的。
更改为
".Its stats are now"