模板类的前向声明(访客设计模式) [英] Forward Declaration of Template Class (Visitor Design Pattern)

查看:333
本文介绍了模板类的前向声明(访客设计模式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图向前声明一个模板类A<T>,供在类Visitor中使用.就我的目的而言,声明类Aint实例A<int>就足够了.我尝试了两种方法,但都给出了不同的错误,而且我不知道如何进行.

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.

这是我的错误的MWE:

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

感谢您的帮助.

推荐答案

我认为您在这里混用东西,应该将accept方法声明为:

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);
}

因为类A是模板.然后,您可以专注于任何类型.

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

这篇关于模板类的前向声明(访客设计模式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆