2013-04-21 176 views
0

我想为我的游戏制作一个输出字符串。这会获取对象ID以及能量级别。有没有一种方法,使之成为一个字符串,这个用变量创建一个字符串

string Ouput = objects[x]->getIdentifier() + "Had the greater energy -> object" + objects[i]->getIdentifier() + "was deleted" + endl; 

感谢

JG

编辑:则getIdentifier的回报()是一个字符。它的排序,所以A,B ... Z

+1

什么是字符串? – 2013-04-21 18:25:04

+2

使用'std :: string'。 – 2013-04-21 18:25:50

+0

我已经有了#include命名空间std;和#include 在顶部。这不够吗? – KingJohnno 2013-04-21 18:27:08

回答

4

不要+endl为一个字符串。如果您需要换行,请改用'\n'

#include <string> 
using namespace std; 

... 

string Ouput = objects[x]->getIdentifier() + .... + "was deleted\n"; 
                   ^^ 

 

如果getIdentifier()返回类型是一个数字,你可以用std::to_string将其转换。

string Ouput = to_string(objects[x]->getIdentifier()) + .... + "was deleted\n"; 
       ^^^^^^^^^ 

如果它是一个char您可以使用下面的方法:

string Ouput = string(1, objects[x]->getIdentifier()) + .... + "was deleted\n"; 
+0

谢谢:-)当我调试代码时,只显示标识符。 – KingJohnno 2013-04-21 18:34:52

+0

@KingJohnno请向我们展示一个说明您的问题的完整示例。请务必只包含重新生成确切问题的代码,不要再提供。还包括示例输入和输出。 – 2013-04-21 18:39:18

+0

我收到错误“无法添加两个指针”。 - 出现问题是因为没有任何内容输出到屏幕上。 (理想情况下,我想写这个文件作为一个字符串) – KingJohnno 2013-04-21 18:45:02

1

如果你想要一个标识符采取两个字符串和int在其穿过的功能。你可以说无效

void getIdentifier(int id, string description) 
{ 
cout << "What is the id\n"; 
cin >> id; 

cout << "What is the description\n"; 
cin >> description; 
} 

然后cout他们两个。

我希望这会有所帮助。

+2

这是行不通的。您正在通过值来传递“description”。您需要通过引用它来传递它,您将其用作返回值。 'std :: string getIdentifier(int i)'可能会更好。 – 2013-04-21 18:38:40

相关问题