具有多个参数和符号的运算符重载
Operator overloading with multiple parameters and symbol
我有一个类:
class taphop
{
public:
taphop();
~taphop();
taphop(const list&);
taphop& operator=(const taphop&);
taphop operator+(const int&);
taphop operator+(const taphop&);
};
在main
中,我不能使用多个参数:
main()
{
taphop lmc=lmc+2+5+3+2; // Error;
lmc=lmc+3+5+2; // Error;
int a=a+1+2+4+5; // Not error;
a=a+1+2+4+5; // Not error;
return 0;
}
有一点是肯定的。以下未定义。
taphop lmc=lmc+2+5+3+2;
由于运算符+将被调用在表达式中假定为完全构造对象的框架上
lmc+2+5+3+2
除了使用lmc初始化lmc之外,您只需要读取编译器给您的错误消息。以下是我对你可能试图做的事情的看法:
class taphop
{
int value;
public:
taphop(const int x = 0) :value(x) {} // default value of 0
~taphop() {}
int getvalue() const { return value; }
taphop& operator=(const taphop &r) { value = r.value; return *this; }
taphop operator+(const int x) { return taphop(value + x); }
taphop operator+(const taphop &r) { return taphop(value + r.value); }
};
int main()
{
taphop lmc = taphop(0) + 2 + 5 + 3 + 2;
std::cout << lmc.getvalue() << std::endl; // prints 12
lmc = lmc + 3 + 5 + 2;
std::cout << lmc.getvalue() << std::endl; // prints 22
return 0;
}
相关文章:
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- 应用于无符号类型的一元减号运算符
- 如何使用 std::copy 当类有"无符号字符和运算符[]"时
- 迭代器和无符号整数的重载 + 运算符
- 预处理器是否可以更改运算符重载功能的符号?
- 在 tensorflow-GPU 中使用用户运算符时未定义的符号>=1.15
- 在浮点值上否定运算符与提升::数学::更改符号
- 由于无符号算术运算符的环绕而导致的 MISRA 错误
- 为什么我不能使用在 Visual C++ 32 位中实现运算符无符号 int() 作为数组索引的类?
- C++11 无符号字符在使用运算符 = 时变为 int
- 移位运算符如何影响无符号数字
- 运算符过载上未解决的外部符号
- 如何解决具有多个运算符的反向抛光符号?
- C++14 是否定义了无符号 int 的填充位上的按位运算符的行为
- 用户输入逻辑运算符符号或数字,如果输入字符或字符串则无效
- 如何修复此 OpenCV 错误:断言失败((无符号)i < (无符号)cn) 在 cv::Vec<unsigned char,3>:::运算符 (), 文件
- 崩溃时间(运算符 new(无符号 int)+22)
- 在范围解析运算符的左侧使用&符号
- Windows 上的 GMP 库 - 链接错误 LNK2001 未解析的外部符号运算符<<
- 未定义的符号..运算符 new(无符号长整型)