【发布时间】:2014-02-08 14:35:47
【问题描述】:
#include <iostream>
#include <string>
using namespace std;
std::string dispCard(int card)
{
string textCard = "help";
//cout << textCard;
system("pause");
return textCard;
}
int main()
{
// Using this area to test functions for now
cout << dispCard(14);
return 0;
}
取消注释cout 行实际上会显示该值。但我无法返回字符串中的值。
老实说,我不知道为什么这不起作用。我最初只是想使用“char”,但由于某种原因这不起作用。
Visual Studio 不喜欢:
char test;
test = "help";
它在“=”下划线。
现在,我只想从函数返回一个字符串值。我还需要它做更多事情,但这是目前的主要问题。
【问题讨论】:
-
1.如果你需要一个 c 风格的字符串 2,你需要
const char* test = "help。如果你想返回一个字符串,我想你可以做return std::string("help")。 -
没有返回字符串是什么意思?以及为什么在返回之前有
system("pause")? -
请在控制台/终端中测试/启动您的程序并摆脱
system("pause");