0
所以我正在处理一个问题,这是给出的信息,但无论什么原因,它不会编译。我完全从教科书中复制,并且在实现文件中出现错误,例如const(非成员函数中不允许使用类型限定符)和value(成员不可访问)。我猜这只是一个简单的错字,或者包含在顶部的东西,但我无法弄清楚。分类列表,实现文件
// SPECIFICATION FILE (itemtype.h)
const int MAX_ITEM = 5 ;
enum RelationType { LESS, EQUAL, GREATER } ;
class ItemType // declares class data type
{
public : // 3 public member functions
RelationType ComparedTo (ItemType) const;
void Print() const;
void Initialize(int number);
private:
int value;
};
// IMPLEMENTATION FILE (itemtype.cpp)
// Implementation depends on the data type of value.
#include “itemtype.h”
#include <iostream>
using namespace std;
RelationType ComparedTo (ItemType otherItem) const
{
if (value < otherItem.value)
return LESS ;
else if (value > otherItem.value)
return GREATER ;
else
return EQUAL ;
}
void Print () const
{ cout << value << endl ;
}
void Initialize (int number)
{
value = number ;
}
你需要告诉他们属于一个类中的方法实现。例如“RelationType ItemType :: ComparisonTo(ItemType otherItem)const”,而不是“RelationType ComparisonTo(ItemType otherItem)const” – randooom