【发布时间】:2015-11-05 07:01:31
【问题描述】:
所以我正在解决一个问题,这是给定的信息,但无论出于何种原因,它都无法编译。我完全从教科书中复制,但在实现文件中出现错误,例如 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::ComparedTo (ItemType otherItem) const”而不是“RelationType CompareTo (ItemType otherItem) const”
标签: c++ compiler-errors sortedlist