2015-11-05 18 views
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 ;    
} 
+0

你需要告诉他们属于一个类中的方法实现。例如“RelationType ItemType :: ComparisonTo(ItemType otherItem)const”,而不是“RelationType ComparisonTo(ItemType otherItem)const” – randooom

回答

2

有两种可能性:要么你的书是错误的,或者你没有复制代码准确

当成员函数的类定义的外部定义,你需要告诉他们属于哪一类的编译器:

RelationType ItemType::ComparedTo(ItemType otherItem) const 
{ 
    // ... 
} 

// ... 

void ItemType::Print() const 
{ 
    // ... 
} 

等。

(有类,头文件,而且从C++的角度实施flie之间没有关系。)