构造函数和运算符不起作用
Constructors and operators in not working
我正在尝试学习C++,我不明白为什么以下代码不起作用:
class String
{
public:
String();
String(const String& other);
String& operator = (const String& other);
String& operator = (const wchar_t* other);
String& operator () (const wchar_t* other);
~String();
operator const wchar_t* ();
...
在主函数中的某个地方:
wchar_t* x = L"A test string";
String y = (String)x; //not working
String z = x; //not working
VC++编译器告诉我:
Error 1 error C2440: 'type cast': cannot convert from 'wchar_t *' to 'String'
Error 2 error C2440: 'initializing': cannot convert from 'wchar_t *' to 'String'
IntelliSense: no suitable constructor exists to convert from "wchar_t *" to "String"
我做错了什么?
你需要一个构造函数来wchar_t*
。
String(const wchar_t*);
"main中的某个地方"的三行都没有使用赋值,所以我们可以忽略您可能已定义的任何赋值运算符。而且您还没有定义转换构造函数,它需要单个参数(wchar_t const*
),以转换您的wchar_t
const*
。
相关文章:
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 重载运算符 (<<) cout 在 c ++ 中不起作用,当我互相减去两个矩阵时不起作用
- std::string == 运算符在代码中不起作用
- C++11+=当RHS有多个字符串或字符时,运算符不起作用
- 运算符的全局过载<<不起作用,为什么?
- 自定义类型转换运算符在转发引用上调用时不起作用(当对象按值传递时有效)
- 为什么重载运算符>在参数声明 const 时不起作用?
- 重载"+="运算符:c+=a_times_b 有效,但 c+=a*b 不起作用?
- C++宏"##"在"->"运算符后不起作用
- 为什么“运算符<<(cout,double)”不起作用
- 相等运算符优先级不起作用
- C++ - 父级中的 CRTP 分配运算符不起作用
- 隐式用户定义的转换不起作用,因为在编译C 时无法识别运算符和转换构造函数
- 赋值不起作用,但带有取消引用运算符的地址起作用?
- C++ 运算符重载 += 有效,但<<不起作用
- 需要使用 Book* 头变量重载运算符,但不起作用
- C++ 运算符删除重载对派生类不起作用
- 为什么重载运算符上的异常说明符'<<'不适用于任何 std::ostream 对象,但对库中定义的运算符不起作用?
- 运算符重载如何工作,为什么在我的情况下不起作用?
- 为什么COUT在朋友函数中不起作用,该功能超载了操作员&lt;&lt;这是一个iStream运算符