2 oqqsoap1234567 oqqsoap1234567 于 2015.06.01 17:38 提问

图中断点处,85行显示没问题,但94行会报错,为什么?

图片说明
#include
#include
#include
using namespace std;

const int DefaultSize = 50;
template
class bitSet
{
public:
bitSet(int sz = DefaultSize);
bitSet(const bitSet& R);
~bitSet(){delete []bitVector;}
void makeEmpty()
{ for (int i = 0; i < vectorSize; i++) bitVector[i] = 0;}
unsigned short getMember(const T x);
void putMember(const T x, unsigned short v);
bool addMember(const T x);
bool delMember(const T x);
bitSet& operator + (const bitSet& R);
bitSet& operator * (const bitSet& R);
bitSet& operator - (const bitSet& R);
bool Contains(const T x);
bool subSet(bitSet& R);

private:
int setSize;
int vectorSize;
unsigned short *bitVector;
};

template
bitSet::bitSet(int sz):setSize(sz)
{
assert(setSize > 0);
vectorSize = (setSize+15)>>4;
bitVector = new unsigned short[vectorSize];
assert(bitVector != NULL);
for(int i=0; i<vectorSize; i++)bitVector[i] = 0;
};

template
bitSet::bitSet(const bitSet& R)
{
setSize = R.setSize;
vectorSize = R.vectorSize;
bitVector = new unsigned short[vectorSize];
assert(bitVector != NULL);
for (int i = 0; i < vectorSize; i++)
bitVector[i] = R.bitVector[i];
};

template
unsigned short bitSet::getMember(const T x)
{ int ad = x/16, id = x%16;
unsigned short elem = bitVector[ad];
return ((elem >>(15-id)) %2);
}

template
void bitSet::putMember(const T x, unsigned short v)
{ int ad = x/16, id = x%16;
unsigned short elem = bitVector[ad];
unsigned short temp = elem>>(15-id); elem=elem<<(id+1);
if (temp%2 == 0 && v == 1) temp = temp+1;
else if (temp%2 == 1 && v == 0) temp = temp - 1;
bitVector[ad] = (temp<<(15-id)) | (elem>>(id+1));
};

template
bool bitSet::addMember(const T x)
{ assert(x >= 0 && x < setSize);
if (getMember(x) == 0)
{ putMember(x,1); return true;}
return false;
};

template
bool bitSet::delMember(const T x)
{ assert(x >= 0 && x < setSize);
if (getMember(x) == 1)
{ putMember(x,0); return true;}
return false;
}

template
bitSet& bitSet::operator + (const bitSet& R)
{ assert(vectorSize == R.vectorSize);
bitSet temp;
for (int i = 0; i < vectorSize; i++)
temp.bitVector[i] = bitVector[i] | R.bitVector[i];
return temp;
}

template
bitSet& bitSet::operator * (const bitSet& R)
{ assert(vectorSize == R.vectorSize);
bitSet temp(vectorSize);
for (int i = 0; i < vectorSize; i++)
temp.bitVector[i] = bitVector[i] & R.bitVector[i];
return temp;
}

template
bitSet& bitSet::operator - (const bitSet& R)
{ assert(vectorSize == R.vectorSize);
bitSet temp(vectorSize);
for (int i = 0; i < vectorSize; i++)
temp.bitVector[i] = bitVector[i] & !R.bitVector[i];
return temp;
}

template
bool bitSet::Contains(const T x)
{ assert(x >= 0 && x <= setSize);
return (getMember(x) == 1)? true: false;
}

int main()
{
bitSet a;
bitSet b;
return 0;
}

3个回答

gamefinity
gamefinity   Rxr 2015.06.01 18:47
已采纳
  • 你的bitset是一个模板类。所应用它来实例化一个变量的时候要带上参数(一个类型)。
  • 至于说为社么定义的时候不要参数,那是因为他认为参数就是你的class T,既然已经明确没那就不要了
gamefinity
gamefinity 回复oqqsoap1234567: 是的。在这里bitset就是butset<T>
2 年多之前 回复
oqqsoap1234567
oqqsoap1234567 operater + 是一个函数,如果我调用这个函数,在这个函数里再定义一个bitSet<char> temp不行吗,是不是如果不填写<>,默认就是已经定义的classT
2 年多之前 回复
caozhy
caozhy   Ds   Rxr 2015.06.01 17:47

要看你完整的代码。全部贴出来。

oqqsoap1234567
oqqsoap1234567   2015.06.01 18:54

#include
#include
#include
using namespace std;

const int DefaultSize = 50;
template
class bitSet
{
public:
bitSet(int sz = DefaultSize);
bitSet(const bitSet& R);
~bitSet(){delete []bitVector;}
void makeEmpty()
{ for (int i = 0; i < vectorSize; i++) bitVector[i] = 0;}
unsigned short getMember(const T x);
void putMember(const T x, unsigned short v);
bool addMember(const T x);
bool delMember(const T x);
bitSet& operator + (const bitSet& R);
bitSet& operator * (const bitSet& R);
bitSet& operator - (const bitSet& R);
bool Contains(const T x);
bool subSet(bitSet& R);

private:
int setSize;
int vectorSize;
unsigned short *bitVector;
};

template
bitSet::bitSet(int sz):setSize(sz)
{
assert(setSize > 0);
vectorSize = (setSize+15)>>4;
bitVector = new unsigned short[vectorSize];
assert(bitVector != NULL);
for(int i=0; i<vectorSize; i++)bitVector[i] = 0;
};

template
bitSet::bitSet(const bitSet& R)
{
setSize = R.setSize;
vectorSize = R.vectorSize;
bitVector = new unsigned short[vectorSize];
assert(bitVector != NULL);
for (int i = 0; i < vectorSize; i++)
bitVector[i] = R.bitVector[i];
};

template
unsigned short bitSet::getMember(const T x)
{ int ad = x/16, id = x%16;
unsigned short elem = bitVector[ad];
return ((elem >>(15-id)) %2);
}

template
void bitSet::putMember(const T x, unsigned short v)
{ int ad = x/16, id = x%16;
unsigned short elem = bitVector[ad];
unsigned short temp = elem>>(15-id); elem=elem<<(id+1);
if (temp%2 == 0 && v == 1) temp = temp+1;
else if (temp%2 == 1 && v == 0) temp = temp - 1;
bitVector[ad] = (temp<<(15-id)) | (elem>>(id+1));
};

template
bool bitSet::addMember(const T x)
{ assert(x >= 0 && x < setSize);
if (getMember(x) == 0)
{ putMember(x,1); return true;}
return false;
};

template
bool bitSet::delMember(const T x)
{ assert(x >= 0 && x < setSize);
if (getMember(x) == 1)
{ putMember(x,0); return true;}
return false;
}

template
bitSet& bitSet::operator + (const bitSet& R)
{ assert(vectorSize == R.vectorSize);
bitSet temp;
for (int i = 0; i < vectorSize; i++)
temp.bitVector[i] = bitVector[i] | R.bitVector[i];
return temp;
}

template
bitSet& bitSet::operator * (const bitSet& R)
{ assert(vectorSize == R.vectorSize);
bitSet temp(vectorSize);
for (int i = 0; i < vectorSize; i++)
temp.bitVector[i] = bitVector[i] & R.bitVector[i];
return temp;
}

template
bitSet& bitSet::operator - (const bitSet& R)
{ assert(vectorSize == R.vectorSize);
bitSet temp(vectorSize);
for (int i = 0; i < vectorSize; i++)
temp.bitVector[i] = bitVector[i] & !R.bitVector[i];
return temp;
}

template
bool bitSet::Contains(const T x)
{ assert(x >= 0 && x <= setSize);
return (getMember(x) == 1)? true: false;
}

int main()
{
bitSet a;
bitSet b;
return 0;
}

caozhy
caozhy 贴代码的时候外面包裹上`,或者用工具栏上的代码按钮。你这么贴丢失很多字符,代码没法看。
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片