我有一个名为Fstring
的类,它有一个wchar_t*
。C++中的字符串重载操作符
我写了下面的字符串文字复制到Fstring
:
#include <iostream>
using namespace std;
class Fstring{
wchar_t *arr;
public:
Fstring& operator = (const wchar_t temp[])
{
delete [] arr;
arr=new wchar_t[wcslen(temp)];
for(int i=0;i<=wcslen(temp);i++)
arr[i]=temp[i];
return *this;
}
};
int main()
{
Fstring test=L"Hello World";
return 0;
}
但没有奏效。编译器给了我以下错误:
error C2440: 'initializing' : cannot convert from 'const wchar_t [12]' to 'Fstring'
我真的很困惑,我用Google搜索“运算符重载”,但所有的结果都有我用重载操作方式相同。那么为什么这不起作用呢?
为什么不使用'std :: wstring'? – cdhowie
'Fstring test = L“Hello World”;'在构建新的'Fstring'时调用构造函数而不是'operator ='。 –
你也可能想实现一个构造函数。 –