2015-11-08 100 views
0

我試圖在.cpp文件中實現一個.h文件中的虛函數。這是一個任務,所以我不能讓這個功能非虛擬化。這是一個克隆函數,它調用類的拷貝構造函數。功能:C++中的克隆函數

virtual Item* clone() const; 

是班上

Ingredient : public Item { 

當我在Ingredient.cpp文件實現它,我有:

Ingredient::clone() const { 
    return new Ingredient (*this); 
} 

但是,當我嘗試編譯,我得到這兩個錯誤:

Ingredient.cpp:23:13:錯誤:C++需要所有聲明的類型說明符

Ingredient::clone() const { 
     ^

Ingredient.cpp:24:9:錯誤:不能與類型的右值 '成分*' 中產生

return new Ingredient (*this); 
      ^~~~~~~~~~~~~~~~~~~~~~~~ 

2錯誤初始化類型 'INT' 的返回對象。

我不明白我在做什麼錯在這裏,因爲我應該利用這個自引用*這個自指針。有什麼建議麼?

+0

缺少返回類型的函數簽名.. http://ideone.com/J0U8wv – Brandon

回答

1

您忘記了函數實現中的返回類型。

Item* Ingredient::clone() const { 
// ^^ Missing 
+0

這麼簡單的東西......但是這就是問題所在。謝謝! – ComputerScientist123

+0

@camcovington,不客氣。 –