C++ 无法从“std::string”转换为“LPSTR”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3906515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:02:17  来源:igfitidea点击:

cannot convert from 'std::string' to 'LPSTR'

c++windows

提问by Simsons

As I clould not pass LPCSTR from one function to another (Data get changed) I tried passing it as a string.

由于我无法将 LPCSTR 从一个函数传递到另一个函数(数据已更改),因此我尝试将其作为字符串传递。

But later I need to again convert it back to LPSTR. While trying the conversion I am getting the above error:

但稍后我需要再次将其转换回 LPSTR。在尝试转换时,我收到上述错误:

cannot convert from 'std::string' to 'LPSTR'

无法从“std::string”转换为“LPSTR”

How can I resolve this?

我该如何解决这个问题?

回答by Keynslug

That's just because you should use std::string::c_str()method.

那只是因为你应该使用std::string::c_str()方法。

But this involves const_castin given case because const char *returned by c_str()can not be assigned to a non-constant LPSTR.

但这涉及const_cast给定的情况,因为const char *返回的 byc_str()不能分配给非常量LPSTR

std::string str = "something";
LPSTR s = const_cast<char *>(str.c_str());

But you must be sure that lifetime of strwill be longer that that of LPTSTRvariable.

但是你必须确保 的生命周期str会比LPTSTR变量的生命周期长。

Another mention, if code compiles as Unicode-conformant, then types LPTSTRand std::stringare incompatible. You should use std::wstringinstead.

另外要提到的是,如果代码编译为符合 Unicode 的,则类型LPTSTRstd::string不兼容。你应该std::wstring改用。

Important note:If you pass the resulting pointer sfrom above to a function which tries to modify the data it is pointing to this will result in undefined behaviour. The only way to properly deal with it is to duplicate the string into a non-const buffer (e.g. via strdup)

重要说明:如果您将结果指针s从上面传递给试图修改它指向的数据的函数,将导致未定义的行为。正确处理它的唯一方法是将字符串复制到非常量缓冲区中(例如 via strdup

回答by Pedro d'Aquino

If you need an LPSTR, that means the string will/may be modified. std::string::c_str()returns a constpointer, and you can't just const_castit away and hope all is good in the world, because it isn't. The string may be changed in all sorts of nasty ways, and your original std::stringwill be oblivious to all of them.

如果您需要LPSTR,则意味着该字符串将/可能会被修改。std::string::c_str()返回一个const指针,你不能把const_cast它扔掉并希望世界上一切都好,因为事实并非如此。字符串可能会以各种令人讨厌的方式进行更改,而您的原件std::string将忽略所有这些。

Try this instead:

试试这个:

// myFunction takes an LPSTR
std::string cppString = "something";
LPSTR cString = strdup( cppString.c_str() );
try {
   myFunction( cString );
   cppString = cString;
} catch(...) {
   free( cString );
}

Wrap the string in a smart pointer and get rid of the try...catchfor bonus points (don't forget the custom deleter).

将字符串包裹在智能指针中并去掉try...catchfor 奖励积分(不要忘记自定义删除器)。

回答by rerun

There is a function on std::string c_str(). However I doubt that you could not use a std::string in your case.

std::string c_str()上有一个函数。但是我怀疑你不能在你的情况下使用 std::string 。

回答by C Johnson

An LPSTR can be substituted with by using a TCHAR (i.e. found in tchar.h). So if you have a std::string, you can use the method std::string::c_str().

LPSTR 可以通过使用 TCHAR(即在 tchar.h 中找到)来替换。所以如果你有一个 std::string,你可以使用 std::string::c_str() 方法。

回答by ironic

If the function, you are calling does not write to string, but only reads it, then you can simply use string::c_str method. If it is going to write something, then you probably should ensure that your string has enough space by calling string::reserve().

如果您调用的函数不写入字符串,而只读取它,那么您可以简单地使用 string::c_str 方法。如果它要写一些东西,那么你可能应该通过调用 string::reserve() 来确保你的字符串有足够的空间。

回答by Prof. Falken contract breached

Are you running somestringvariablename.c_str()? That should work.

你在跑步somestringvariablename.c_str()吗?那应该工作。