Pergunta

I'm looking to create a non-type templated class with member variables that depend on the non-type parameter (specifically, fixed-dimension Eigen matrices, but the problem is present with int as well). To make things clearer I typedef'ed the member types, which worked great until I wanted a member function to return the typedef at which point I started getting the following error:

myClass.cpp:10: error: expected constructor, destructor, or type conversion before ‘myClass’

I understand, conceptually at least, that this has something to do with the fact that my typedef depends on the template and that as a result C++ is confused. The problem is I'm even more confused, I've tried some naive insertions of typename, but that didn't fix anything.

A minimum working example.

Header:

template <int i> class myClass
{
  public:
    typedef int myVector_t;

    myClass();
    myVector_t myFunc();
};

Source code:

#include <myClass.hpp>

template <int i>
myClass<i>::myClass()
{
  //blah
}

template <int i>
myClass<i>::myVector_t  myClass<i>::myFunc()        //<----- Line 10
{
  //blah
}

I'd appreciate any insight.

Edit: Answer

As explained below the solution is to include the typename keyword in the implementation, but not the declaration.

typename myClass<i>::myVector_t  myClass<i>::myFunc()        //<----- Line 10

Edit2

Generalized the question away from Eigen

Foi útil?

Solução

Since the name myVector_t in the definition of the function effectively depends on a template parameter, you need to let the compiler know it's a type with typename:

template <int i>
typename myClass<i>::myVector_t  myClass<i>::myFunc()        //<----- Line 10
{
  //blah
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top