[英]Convert const wchar_t* into a WCHAR*
How do I make this to work? 我该如何使它工作? C++ types are really confusing: C ++类型真的很混乱:
std::wstring wquery = std::wstring(query.begin(), query.end());
//split names
std::vector<WCHAR*> split_names;
std::stringstream ss;
ss.str(names);
std::string name;
while (std::getline(ss, name, ',')) {
split_names.push_back(
(
std::wstring(
name.begin(),
name.end()
)
).c_str()
); //error can't assign const wchar_t* into WCHAR*
}
C++ tries to keep you from mistakes. C ++试图让你远离错误。 Here: 这里:
std::wstring(name.begin(), name.end())).c_str()
you create a temporary object std::wstring
and get the pointer to the string content. 你创建一个临时对象std::wstring
并获取指向字符串内容的指针。 Object will be destroyed right after you leave this block. 离开此区块后,对象将被销毁。 As a result you will get an invalid pointer. 因此,您将获得无效指针。
Don't store pointer to the temporary object into your std::vector<WCHAR*> split_names;
不要将指向临时对象的指针存储到std::vector<WCHAR*> split_names;
. 。
I solved it rewriting everything to: 我解决了它重写所有内容:
//split names
std::vector<std::wstring> split_names;
std::stringstream ss;
ss.str(names);
std::string name;
while (std::getline(ss, name, ',')) {
split_names.push_back(std::wstring(name.begin(), name.end()));
}
std::vector<const WCHAR*> pszTags;
pszTags.resize(split_names.size());
for (int i = 0; i < pszTags.size(); i++)
pszTags[i] = split_names[i].c_str();
Sorry for the inconvenience. 抱歉给你带来不便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.