我想从C++第2卷思维进行以下锻炼,巫婆说:模板练习题
在下面的代码的类不可比不具有运算符=()。为什么类HardLogic的存在会导致编译错误,但SoftLogic不会呢?
#include <iostream>
using namespace std;
class NonComparable {};
struct HardLogic {
NonComparable nc1, nc2;
void compare()
{
return nc1 == nc2;
}
};
template<class T>
struct SoftLogic {
NonComparable nc1, nc2;
void noOp() {}
void compare()
{
nc1 == nc2;
}
};
int main()
{
SoftLogic<NonComparable> l;
l.noOp();
return 0;
}
1)HardLogic ::比较返回void但函数试图O返回一个INT /布尔。
2)SoftLogic也有一些奇怪的东西(对我来说):nc1 == nc2。
3)练习说关于operator =(),但在代码中使用operator ==()。
是否有错误?我发现在这样一本书的代码中出现如此多的错误是很奇怪的,所以我错过了什么?有没有人遇到过这个练习?
我有gcc编译器,我总是得到一个错误,即使我评论HardLogic类 – Kobe 2011-05-05 16:59:27