2015-12-20 32 views
-1

我想创建一个Animal界面。 Cat类实现它。一只动物可以吃另一只动物eat(Animal a)。如果动物死亡,Animal.die()会杀死动物,并且Animal.isDead()返回。在C++中实现类似java的界面

如果我想编译它,我得到了一些错误:

templates may not be 'virtual' 
invalid use of incomplete type 'class A' 
expected class-name before '{' token 

我已经搜查了很多,如何解决这个问题的错误。但他们都没有解决它。我不是C++专家。我只有几年的JAVA经验。

代码:

所有的
#include <iostream> 

using namespace std; 

//interface 
class Animal { 

public: 
    template<class A extends Animal> 
    virtual void eat(A* a) = 0; 
    virtual void die() = 0; 
    virtual bool isDead() = 0; 

}; 

// Cat class 
class Cat: Animal { 

private: 
    bool dead = false; 

public: 
    Cat(); 
    Cat(const Cat& orig); 
    virtual ~Cat(); 

    template<class A extends Animal> 
    void eat(A* a); 
    void die(); 
    bool isDead(); 

}; 

// Implement cat 
Cat::Cat() { 
} 

Cat::Cat(const Cat& orig) { 
} 

Cat::~Cat() { 
} 


template<class A extends Animal> 
void Cat::eat(A* a) { 
    a->die(); 
} 

void Cat::die() { 
    dead = true; 
} 

bool Cat::isDead() { 
    return dead; 
} 

int main(int argc, char** argv) { 

    Cat* cat = new Cat(); 
    Cat* cat2 = new Cat(); 

    cat->eat(cat2); 

    cout << (cat2->isDead()? "Cat2 is dead" : "Cat2 is not dead") << endl; 

    return 0; 
} 
+4

请参阅关于C++的基本书籍。 –

+2

看起来像你在猜测。你不能以这种方式学习'C++'。我建议通过一本好书有条不紊地工作:https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Galik

+0

尽管在思考的同时尝试编写Java是一个糟糕的主意在C++中,在Java中思考和尝试编写C++也是一个不好的主意。许多精心设计的C++程序远不是几乎纯粹的OO方法,这是Java的典型做法。 – cdonat

回答

3

你的问题是与基因RICS。它们与模板不同,即使它们的语法有些类似。

替换:

template<class A extends Animal> 
virtual void eat(A* a); 

有了:

virtual void eat(Animal*); 

而且你的代码编译。 Java基本上完成了上述操作,但是当您调用eat时,它将存储运行时类型A,并且在某些情况下会将其重新转换为该类型。在C++中,你有责任为你自己施放。

您可以使用模板tomfoolery复制Java对泛型的大部分或全部内容,但它很少值得费心。

1

Firts必须指定公有继承语句中使用您的Animal接口多态性的方式。

class Cat: public Animal 

其次C++模板声明略微从Java

template <class T> 
class ClassName 
{ 
    void ClassMethod(T* pObject); 
}; 

的区别方法defenition

template <class T> 
void ClassName<T>::ClassMethod(T* pObject) 
{ 
} 

,所以你的代码必须出现这样

template <class A> 
class Animal { 

public: 
    virtual void eat(A* a) = 0; 
    virtual void die() = 0; 
    virtual bool isDead() = 0; 

}; 

// Cat class 
template <class A> 
class Cat: public Animal<A> { 

private: 
    bool dead = false; 

public: 
    Cat(); 
    Cat(const Cat<A>& orig); 
    virtual ~Cat(); 
    void eat(A* a); 
    void die(); 
    bool isDead(); 

}; 

// Implement cat 
template <class A> 
Cat<A>::Cat() { 
} 

template <class A> 
Cat<A>::Cat(const Cat<A>& orig) { 
} 

template <class A> 
Cat<A>::~Cat() { 
} 


template<class A> 
void Cat<A>::eat(A* a) { 
    a->die(); 
} 

template <class A> 
void Cat<A>::die() { 
    dead = true; 
} 

template <class A> 
bool Cat<A>::isDead() { 
    return dead; 
} 

int main(int argc, char** argv) { 

    Cat<Cat>* cat = new Cat<Cat>(); 
    Cat<Cat>* cat2 = new Cat<Cat>(); 

    cat->eat(cat2); 

    cout << (cat2->isDead()? "Cat2 is dead" : "Cat2 is not dead") << endl; 

    return 0; 
} 
相关问题