首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

重载异或运算符,该怎么解决

2012-04-03 
重载异或运算符class Test{private:unsigned crcpublic:Test()Test operator ^(unsigned &)Test(unsign

重载异或运算符
class Test
{
private:
  unsigned crc;
public:
  Test();
  Test operator ^(unsigned &);
  Test(unsigned c);
}

Test::Test():crc(0){}
Test::Test(unsigned c)
{
  crc=c;
}

Test Test::operator ^(unsigned &c)
{
  return Test(this->crc^c);
}

int main()
{
  Test test;
  test=test^2; //编译能不过
  return 0;
}

[解决办法]

C/C++ code
//首先,没有加 ‘;’class Test{};//没有加;test=test^2; //这里报错  no operator defined which takes a right-hand operand of type 'const int'//其次,改成如下  unsigned int t=2;test=test^t; //编译能不过 

热点排行