我有一个名为 的类Fstring,其中有一个wchar_t*。
我写了以下内容将字符串文字复制到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;
}
Run Code Online (Sandbox Code Playgroud)
但它没有用。编译器给了我以下错误:
错误 C2440:“正在初始化”:无法从“const wchar_t [12]”转换为“Fstring”
我真的很困惑,我在谷歌上搜索了“重载运算符”,但所有结果都与我用来重载运算符的方式相同。那么为什么这不起作用呢?