2015-11-05 28 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之間沒有關係。)