C++ `wstring_convert`std::wstring_convert导致错误C2280:尝试引用已删除的函数
我试图实现一个字符串类,该类同时处理std::String和std::wstring,以减少转换代码 在这个类中,我决定使用延迟更新策略。每次只需修改需要更新的字符串。 如果call operator+=const wchar\u t,那么我应该更新wstr:wstring,如果wstring根据wstrUpdated:bool不正确,那么通过调用wstring\u convert使用str:string更新它,然后执行该操作 此时,wstring_convert将无法工作,并告诉错误C2280。 我很想知道是什么导致了这个问题。我该如何解决它 这是我的密码:C++ `wstring_convert`std::wstring_convert导致错误C2280:尝试引用已删除的函数,c++,c++11,C++,C++11,我试图实现一个字符串类,该类同时处理std::String和std::wstring,以减少转换代码 在这个类中,我决定使用延迟更新策略。每次只需修改需要更新的字符串。 如果call operator+=const wchar\u t,那么我应该更新wstr:wstring,如果wstring根据wstrUpdated:bool不正确,那么通过调用wstring\u convert使用str:string更新它,然后执行该操作 此时,wstring_convert将无法工作,并告诉错误C2280
#pragma once
// MutableString.h
#include<string>
#include<codecvt>
#include<locale>
class MutableString
{
public:
MutableString();
~MutableString();
MutableString(const MutableString& other);
MutableString(const std::string& other);
MutableString(const std::string&& other);
MutableString(const std::wstring& other);
MutableString(const std::wstring&& other);
MutableString(const char* other);
MutableString(const wchar_t* other);
void clear();
MutableString& operator = (const MutableString& other);
MutableString& operator += (const wchar_t ch);
MutableString& operator += (const char ch);
std::string getMultiByteString();
std::wstring getWideString();
private:
bool strUpdated;
bool wstrUpdated;
std::string str;
std::wstring wstr;
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
};
//MutableString.cpp
#include "MutableString.h"
MutableString::MutableString()
{
str = "";
wstr = L"";
strUpdated = false;
wstrUpdated = false;
}
MutableString::~MutableString()
{
;
}
MutableString::MutableString(const MutableString& other)
{
strUpdated = other.strUpdated;
wstrUpdated = other.wstrUpdated;
str = other.str;
wstr = other.wstr;
}
MutableString::MutableString(const std::string& other)
{
strUpdated = true;
wstrUpdated = false;
str = other;
}
MutableString::MutableString(const std::string&& other)
{
strUpdated = true;
wstrUpdated = false;
str = other;
}
MutableString::MutableString(const std::wstring& other)
{
strUpdated = false;
wstrUpdated = true;
wstr = other;
}
MutableString::MutableString(const std::wstring&& other)
{
strUpdated = false;
wstrUpdated = true;
wstr = other;
}
MutableString::MutableString(const char* other)
{
strUpdated = true;
wstrUpdated = false;
str = other;
}
MutableString::MutableString(const wchar_t* other)
{
strUpdated = false;
wstrUpdated = true;
wstr = other;
}
void MutableString::clear()
{
strUpdated = true;
wstrUpdated = true;
wstr.clear();
str.clear();
}
MutableString& MutableString::operator = (const MutableString& other)
{
str = other.str;
wstr = other.wstr;
strUpdated = other.strUpdated;
wstrUpdated = other.wstrUpdated;
return *this;
}
MutableString& MutableString::operator += (const wchar_t ch)
{
if (strUpdated == true)
{
if (wstrUpdated == false)
{
wstr = converter.from_bytes(str.c_str());
}
wstrUpdated = true;
wstr += ch;
strUpdated = false;
}
else
{
if (wstrUpdated == true)
{
wstr += ch;
strUpdated = false;
}
}
return *this;
}
MutableString& MutableString::operator += (const char ch)
{
if (wstrUpdated == true)
{
if(strUpdated == false)
{
str = converter.to_bytes(wstr.c_str());
}
strUpdated = true;
str += ch;
wstrUpdated = false;
}
else
{
if (strUpdated == true)
{
str += ch;
wstrUpdated = false;
}
}
return *this;
}
std::string MutableString::getMultiByteString()
{
if (strUpdated == false && wstrUpdated == true)
{
//change
strUpdated = true;
str = converter.to_bytes(wstr.c_str());
return str;
}
else
{
if (strUpdated == false)
return
查看完整错误可能会有所帮助,但我猜wstring\u convert不可复制。@RetiredInja是的,我在本地代码范围中创建了“wstring\u convert”,而不是您所说的类成员,它工作得很好。但是仍然令人困惑。MutableString::MutableStringconst std::string&&other是毫无意义的。把它扔掉。您可以添加MutableString::MutableStringstd::string&&other,但您需要学习移动语义以充分利用它。@R.MartinhoFernandes是的,您是对的。我认为,如果我犯了错误并修改了“std::string&&other”,那么添加“const”会使代码更安全。。但是当我开始思考时…即使修改了右值也不会对任何东西产生影响。。