Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误C2664:&x27;MessageBoxA';:无法将参数2从';std::string';至';LPCSTR&x27;_C++_Winapi - Fatal编程技术网

C++ 错误C2664:&x27;MessageBoxA';:无法将参数2从';std::string';至';LPCSTR&x27;

C++ 错误C2664:&x27;MessageBoxA';:无法将参数2从';std::string';至';LPCSTR&x27;,c++,winapi,C++,Winapi,我用c++试过这个: std::string teststring = "hello"; MessageBox(NULL,teststring,NULL, NULL); 错误C2664:“MeaseBox”:不能将参数2从“STD::String”转换为“LPCSTR”,它看起来像VisualC++那样正确地标记。 您可以使用std::string上的c_str()方法获取内部缓冲区,因此您的代码变成: std::string teststring = "hello"; MessageBox(

我用c++试过这个:

std::string teststring = "hello";
MessageBox(NULL,teststring,NULL, NULL);

错误C2664:“MeaseBox”:不能将参数2从“STD::String”转换为“LPCSTR”

,它看起来像VisualC++那样正确地标记。 您可以使用std::string上的c_str()方法获取内部缓冲区,因此您的代码变成:

std::string teststring = "hello";
MessageBox(NULL,teststring.c_str(),NULL, NULL);

首先,它看起来像VisualC++那样正确地标记它。 您可以使用std::string上的c_str()方法获取内部缓冲区,因此您的代码变成:

std::string teststring = "hello";
MessageBox(NULL,teststring.c_str(),NULL, NULL);
试试这个怎么样

std::string teststring = "hello";
LPCSTR tmp = teststring .c_str()
MessageBox(NULL,tmp ,NULL, NULL);
试试这个怎么样

std::string teststring = "hello";
LPCSTR tmp = teststring .c_str()
MessageBox(NULL,tmp ,NULL, NULL);

MessageBox的第二个和第三个参数需要一个C字符串

要从调用的std::string中获取C字符串,正确的调用方法是:

std::string teststring = "hello";
MessageBox(NULL, teststring.c_str(), NULL, NULL);

MessageBox的第二个和第三个参数需要一个C字符串

要从调用的std::string中获取C字符串,正确的调用方法是:

std::string teststring = "hello";
MessageBox(NULL, teststring.c_str(), NULL, NULL);

为什么是Visual C++?你可以在这里使用任何C++编译器。但是答案是正确的)是的,但是通常在Windows环境中使用Windows API(MeaseBox)时,很可能是在VisualC++上。这就是为什么我说,“为什么看起来像Visual C++?你可以在这里使用任何C++编译器。但是答案是正确的)是的,但是通常在Windows环境中使用Windows API(MeaseBox)时,很可能是在VisualC++上。这就是为什么我说,'看起来'最后一个参数甚至不是指针,但是你传递
NULL
@chris,NULL实际上是有效的。如果它能工作,我对它没有问题。0或
MB_OK
也是如此。最后一个参数甚至不是指针,但是你传递
NULL
@chris-Well,NULL实际上可以工作。如果它能工作,我对它没有问题。0或
MB_OK
也是如此。