该代码被写入以实现具有一些常用功能的Bit类。C++代码错误
#include <iostream>
#include <math.h>
using namespace std;
class Bit
{
int width;
int value;
public:
Bit(int v, int w)
{
value=v;
width=w;
}
Bit(const Bit& b)
{
value= b.value;
width= b.width;
}
int getWidth()
{
return width;
}
int getValue()
{
return value;
}
Bit plus(int newval)
{
value+=newval;
if(value>=pow(2,width))
cout<<"Overflow";
return this;
}
};
的错误信息是:
Conversion from 'Bit* const' to non-scalar type 'Bit' requested.
我怎么能删除错误?
如果我将构造函数更改为'Bit(Bit b) { value = b.value; width = b.width; } '为什么这里错了,但它在java中完美工作。 – 2012-02-17 14:02:47
复制构造函数必须通过引用采用其参数;为了按值传递它,你需要拷贝构造函数来创建值 - 这是不可能的,因为这是*拷贝构造函数。在Java中,参数是一个参考 - 你不能通过值传递类类型。在C++中,你必须声明它是一个引用,'Bit(Bit const&b)'。 – 2012-02-17 14:08:15