简体   繁体   English

“MessageBoxA”:无法将参数 2 从“std::vector<_Ty>”转换为“LPCSTR”

[英]'MessageBoxA' : cannot convert parameter 2 from 'std::vector<_Ty>' to 'LPCSTR'

The following code works:以下代码有效:

void CMyPlugin8::myMessageBox(std::string& myString)
{
    myString = "Received the following string\n" + myString;
    char * writable = new char[myString.size() + 1];
    std::copy(myString.begin(), myString.end(), writable);
    writable[myString.size()] = '\0'; // don't forget the terminating 0 "delete[] writable;"

    int msgboxID = MessageBox(
        NULL,
        writable,
        "Notice",
        MB_OK
    );
    delete[] writable;
}

To clean up automatically I used info from: How to convert a std::string to const char* or char*?为了自动清理,我使用了以下信息: How to convert a std::string to const char* or char*? . .

The following code throws an error:以下代码会引发错误:

void CMyPlugin8::myMessageBox(std::string& myString)
{
    myString = "Received the following string\n" + myString;
    std::vector<char> writable(myString.begin(), myString.end());
    writable.push_back('\0');

    int msgboxID = MessageBox(
        NULL,
        writable,
        "Notice",
        MB_OK
    );
}

I'm getting this error: 'MessageBoxA': cannot convert parameter 2 from 'std::vector<_Ty>' to 'LPCSTR'我收到此错误: “MessageBoxA”:无法将参数 2 从“std::vector<_Ty>”转换为“LPCSTR”

You cannot pass a vector as LPCSTR, you must. 您不能像LPCSTR那样传递矢量,必须这样做。 Use: 采用:

&writable[0]

or: 要么:

writable.data()

instead. 代替。 Or simply use myString.c_str() 或者只是使用myString.c_str()

MessageBox takes a const char* . MessageBox采用const char* You don't need to copy the string first for that. 您无需为此先复制字符串。 Simply use c_str : 只需使用c_str

void CMyPlugin8::myMessageBox(std::string& myString)
{
    myString = "Received the following string\n" + myString;
    int msgboxID = MessageBox(
        NULL,
        myString.c_str(),
        "Notice",
        MB_OK
    );
}

Note that I think your API is poor: you are modifying the value of the string passed in. Usually the caller wouldn't be expecting that. 请注意,我认为您的API很差:您正在修改传入的字符串的值。通常,调用者不会期望这样。 I think your function should look like this instead: 我认为您的函数应如下所示:

void CMyPlugin8::myMessageBox(const std::string& myString)
{
    std::string message = "Received the following string\n" + myString;
    int msgboxID = MessageBox(
        NULL,
        message.c_str(),
        "Notice",
        MB_OK
    );
}
void CMyPlugin8::myMessageBox(const std::string& myString)
{
    std::string message = "Received the following string\n" + myString;
    int msgboxID = MessageBox(
        NULL,
        message.c_str(),
        "Notice",
        MB_OK
    );
}

Thanks everyone & @Falcon 谢谢大家和@Falcon

If you still face a problem, after changing it myString.c_str().如果您仍然遇到问题,请在更改 myString.c_str() 之后。 Try this, Go to the Properties for your Project and under Configuration Properties/Advanced , change the Character Set to "Not Set" .试试这个, Go 到您的项目的属性,并在Configuration Properties/Advanced下,将 Character Set 更改为"Not Set" This way, the compiler will not assume that you want Unicode characters, which are selected by default:这样,编译器就不会假定您需要 Unicode 个字符,这些字符是默认选中的:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从&#39;std :: vector &lt;_Ty&gt;&#39;转换为&#39;std :: vector &lt;_Ty&gt;& - cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty> & 无法将参数3从&#39;std :: vector &lt;_Ty&gt; *&#39;转换为&#39;库存*&#39;。 为什么? - cannot convert parameter 3 from 'std::vector<_Ty> *' to 'Inventory *'. Why? 接收错误C2664:传递向量时,无法将参数1从&#39;std :: vector &lt;_Ty&gt;&#39;转换为&#39;std :: vector &lt;_Ty,_Ax&gt;&&#39; <int> 模板法 - Receiving error C2664: cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::vector<_Ty,_Ax> &' when passing vector<int> to templated method std :: allocator &lt;_Ty&gt; :: deallocate:无法将参数1从&#39;X&#39;转换为&#39;Y&#39; - std::allocator<_Ty>::deallocate : cannot convert parameter 1 from 'X' to 'Y' 无法从std :: shared_ptr &lt;_Ty&gt;转换参数1(??) - Cannot convert parameter 1 from std::shared_ptr <_Ty> (??) std :: pair &lt;_Ty1,_Ty2&gt; :: pair &lt;_Ty1,_Ty2&gt;&无法转换参数 - std::pair<_Ty1,_Ty2>::pair<_Ty1,_Ty2>& Cannot convert parameter 无法将&#39;this&#39;指针从&#39;std :: stack &lt;_Ty&gt;&#39;转换为&#39;std :: stack &lt;_Ty&gt;&&#39; - Cannot convert 'this' pointer from 'std::stack<_Ty>' to 'std::stack<_Ty> &' 错误C2664:&#39;void std :: vector &lt;_Ty&gt; :: push_back(_Ty &amp;&amp;)&#39;:无法从&#39;Node转换参数1 <T> *&#39;到&#39;节点 <T> &amp;&amp;” - error C2664 : 'void std::vector<_Ty>::push_back(_Ty&&)': cannot convert parameter 1 from 'Node<T> *' to 'Node<T>&&' 错误C2440:&#39;初始化&#39;:无法从&#39;初始化列表&#39;转换为&#39;std :: vector <char *,std::allocator<_Ty> &gt;” - error C2440: 'initializing': cannot convert from 'initializer list' to 'std::vector<char *,std::allocator<_Ty>>' 错误C2440:“正在初始化”:无法从“ std :: _ Vector_iterator &lt;_Ty,_Alloc&gt;”转换为“类型*” - error C2440: 'initializing' : cannot convert from 'std::_Vector_iterator<_Ty,_Alloc>' to 'type *'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM