简体   繁体   中英

How can I override a virtual function with template argument in C++?

How can I override a virtual function with template argument? virtual T accept(Visitor<T>);

#include <iostream>

template<typename T>
class Visitor{
public:
    template<typename K>
    T visit(K);
};

template<typename T>
class Expr{
public:
    virtual T accept(Visitor<T>);
};

template<typename T>
class Lit: public Expr<T>{
public:
    int value;
    Lit(int a){
        value = a;
    }
    T accept(Visitor<T> v) override{
        v.visit(*this);
    }
};

template<typename T>
class Add: public Expr<T>{
public:
    Expr<T> left, right;
    Add(Expr<T> a, Expr<T> b){
        left = a; right = b;
    }
    T accept(Visitor<T> v) override{
        v.visit(*this);
    }
};

class tv: public Visitor<int>{
public:

    int visit(Lit<int> a){
        return a.value;
    }
    int visit(Add<int> a){
        return a.left.accept(*this) + a.right.accept(*this);
    }
};

int main() {
    Add<int> a(Lit<int>(1), Lit<int>(2));
    tv testvisitor;
    std::cout << a.accept(testvisitor);
    return 0;
}

On my end, the failure is the following:

/tmp/ccysNlSn.o: In function `Add<int>::accept(Visitor<int>)':
main.cpp:(.text._ZN3AddIiE6acceptE7VisitorIiE[_ZN3AddIiE6acceptE7VisitorIiE]+0x2b): undefined reference to `int Visitor<int>::visit<Add<int> >(Add<int>)'
/tmp/ccysNlSn.o:(.rodata._ZTV4ExprIiE[_ZTV4ExprIiE]+0x10): undefined reference to `Expr<int>::accept(Visitor<int>)'
/tmp/ccysNlSn.o: In function `Lit<int>::accept(Visitor<int>)':
main.cpp:(.text._ZN3LitIiE6acceptE7VisitorIiE[_ZN3LitIiE6acceptE7VisitorIiE]+0x2b): undefined reference to `int Visitor<int>::visit<Lit<int> >(Lit<int>)'
collect2: error: ld returned 1 exit status

The key errors here are the following:

undefined reference to `int Visitor<int>::visit<Add<int> >(Add<int>)'
undefined reference to `Expr<int>::accept(Visitor<int>)'

These errors mean you have not provided the implementations for int Visitor<int>::visit<Add<int> >(Add<int>) and Expr<int>::accept(Visitor<int>) . So just providing an empty implementation will fix them:

template<typename T>
class Visitor{
public:
    template<typename K>
    T visit(K) {};  // Added an empty body here
};

template<typename T>
class Expr{
public:
    virtual T accept(Visitor<T>) {};  // Added an empty body here
};

This will compile and link fine, but of course you need to supply your actual code to the implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM