Forward Declaration of Template Class (Visitor Design Pattern)

寵の児 提交于 2020-01-06 05:08:49

问题


I am trying to forward declare a templated class A<T> for use in a class Visitor. It would suffice for my purposes to declare the int instance A<int> of the class A. I have tried two approaches but both give different errors, and I don't know how to proceed.

Here is a MWE of my error:

namespace visitor{  
    class Visitor{
    public:
        virtual void visit(nsp::A<int>*) = 0;
    };    
}

namespace nsp{    
    template <class T>
    class A{
        A();
        T t_attribute;          
        void accept(visitor::Visitor*);
    };    

    void A<int>::accept(visitor::Visitor*){
        v -> visit(this);
    }        
}

int main(){
    return 0;
}

You can try running the code here to see the error I get:

error: specializing member 'nsp::A<int>::accept' requires 'template<>' syntax

I appreciate any help.


回答1:


I think you are mixing things here, you should declare accept method as:

template<class T>
void A<T>::accept(visitor::Visitor* v){
    v -> visit(this);
}

as class A is template. Then you can specialize for any type.



来源:https://stackoverflow.com/questions/50078871/forward-declaration-of-template-class-visitor-design-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!