[英]binary operator overloading with class and constant in c++
I am trying to do operator overloading我正在尝试进行运算符重载
My header is:我的 header 是:
class Nyble
{
public:
Nyble();
Nyble(const Nyble& n);
Nyble& operator=(const Nyble& n);
~Nyble();
Nyble operator+(const char a);
Nyble operator-(const char a);
Nyble operator+(Nyble& n1);
Nyble operator+();
unsigned char getData();
private:
// Do not change this data
unsigned char data;
} }
Source:资源:
#include "Nyble.h"
unsigned char Nyble::getData()
{
return this->data;
}
Nyble Nyble::operator+(const char val)
{
return Nyble(getData()+val);
}
Nyble Nyble::operator-(const char value)
{
return Nyble(value + getData()) ;
}``
I am getting error saying no suitable constructor exist to convert int to Nyble.if so what constructor should i declare.Else what changes should i make to the overloading function?我收到错误说没有合适的构造函数来将 int 转换为 Nyble。如果我应该声明什么构造函数。否则我应该对重载的 function 进行哪些更改?
You need to add a constructor for Nyble(getData() + val);
您需要为
Nyble(getData() + val);
and Nyble(value + getData())
to work:和
Nyble(value + getData())
工作:
class Nyble {
public:
explicit Nyble(char d); // add this
// ...
};
Nyble::Nyble(char d) : data(d) {} // and the implementation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.