我试图在.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' 的返回对象。
我不明白我在做什么错在这里,因为我应该利用这个自引用*这个自指针。有什么建议么?
缺少返回类型的函数签名.. http://ideone.com/J0U8wv – Brandon