Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ template question

Tags:

c++

templates

I'm writing some test cases for C++ name-demangling code, and I get a weird error when I try to compile this: (the following is pathologically bad C++ code that I would never use in practice).

template<class U, class V>
class TStruct
{
  U u;
  V v;
public:
  void setU(const U& newu) {u = newu; } 
};

template<class T, class U>
class Oog
{
    T t;
    U u;

public:
    Oog(const T& _t, const U& _u) : t(_t), u(_u) {}
    void doit(TStruct<T,U> ts1, TStruct<U,T> ts2, U u1, T t1) {}

    template<class F>
    class Huh
    {
        F f;
    public:

        template<class V>
        class Wham
        {
            V v;
        public:
            Wham(const V& _v) : v(_v) {}
            void joy(TStruct<T,V> ts1, U u, F f) {}
        };
    };
    int chakka(const Huh<T>::Wham<U>& wu, T t) {}    // error here
};

error is as follows:

 "typetest.cpp", line 165: error: nontype "Oog<T, U>::Huh<F>::Wham [with F=T]"
  is not a template

Any ideas how I can fix?

like image 405
Jason S Avatar asked Sep 16 '11 13:09

Jason S


2 Answers

The correct line should be as,

int chakka(const typename Huh<T>::template Wham<U>& wu, T t) ...
     it's a type ^^^^^^^^         ^^^^^^^^ indicate that 'Wham' is a template

[Note: g++ is quite helpful in this case :) ]

like image 57
iammilind Avatar answered Nov 19 '22 02:11

iammilind


You need to tell it that the Wham member of Huh will be a template:

const Huh<T>::template Wham<U> &
like image 2
Alan Stokes Avatar answered Nov 19 '22 04:11

Alan Stokes