开发者

Can I write an interface for a template class in C++

I have an interface and a couple of implementations of a class that stores serialized objects. I'd like to make the implementation classes into template classes so I can use them with more than one type of object, but I'm getting compiler errors.

#include <iostream>
template<typename T>
class Interface{
public:
    virtual void func(T& c) = 0;
};

class Container{
public:
    Container() : dummy(10){}
    int dummy;
};

开发者_运维百科
template<typename T>
class Implementation : public Interface{
public:
    void func(T& c){
        std::cout << "++c.dummy " << ++c.dummy << std::endl;
    }
};

int main(){
    Container c;
    Implementation<Container> i;
    i.func(c);
    return 0;
}

I get "error: expected class-name before ‘{’ token" at the "class Implementation..." line.

Thanks.


template<typename T>
class Implementation : public Interface<T> {
//                                     ^^^
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜