为什么我不能在我的程序中声明一个字符串:"string is undeclared identifier"



我不能在程序中声明字符串:

string MessageBoxText = CharNameTextBox->Text;

就是行不通。它是string is undeclared identifier。我在名称空间中遗漏了什么或者包含了什么?

确保你已经包含了这个header:

#include <string>

然后用std::string代替string。因为string是在std命名空间中定义的。

不要在命名空间作用域下写这个:

using namespace std; //bad practice if you write this at namespace scope

然而,在函数范围内编写它并没有那么糟糕。但最好的是我之前建议的:

使用std::string 作为:

std::string MessageBoxText = CharNameTextBox->Text;

要在c++中使用标准的string类,您需要#include <string>。一旦你添加了#include指令,string将被定义在std命名空间中,你可以将其引用为std::string

#include <string>
#include <iostream>
int main()
{
    std::string hw( "Hello, world!n" );
    std::cout << hw;
    return 0;
}

您是否以任何方式使用c++/CLI编译,微软的。net扩展,而不是标准的ISO c++ ?

在这种情况下,你应该这样做:

System::String^ MessageBoxText = CharNameTextBox->Text;

请参阅以下文章:

  • 如何:在各种字符串类型之间转换
  • 如何将System::字符串转换为标准字符串
  • 如何:将标准字符串转换为System::String

最新更新