Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ template t is not valid template type

Tags:

c++

templates

My .h file:

template <typename T>
class UpdateUtils
{
public:
typedef struct {
    QList<T> file;
} TPath;

static TPath *getIdealPath(QList<TPath *> &paths);
};

My .cpp file:

template <typename T>
TPath *UpdateUtils<T>::getIdealPath(QList<TPath *> &paths) {
   return 0;
}

This produces errors in cpp file:

error: C2143: syntax error : missing ';' before '*'
error: C2065: 'T' : undeclared identifier
error: C2923: 'UpdateUtils' : 'T' is not a valid template type argument for parameter 'T'

If I replace TPath * return type with e.g. int, it works. Can you please advise?

like image 964
Martin Dusek Avatar asked Jan 02 '23 17:01

Martin Dusek


1 Answers

TPath is a nested class defined inside UpdateUtils, you should qualify it and use typename keyword. e.g.

template <typename T>
typename UpdateUtils<T>::TPath *UpdateUtils<T>::getIdealPath(QList<TPath *> &paths)
^^^^^^^^^^^^^^^^^^^^^^^^^

Or apply trailing return type as @PiotrSkotnicki suggested:

template <typename T>
auto UpdateUtils<T>::getIdealPath(QList<TPath *> &paths) -> TPath *
^^^^                                                     ^^^^^^^^^^

Note that for member function definition out of class definition, names used in parameter-list and trailing-return-type will be looked up in the class scope, so you don't need to qualify them (it's fine to qualify them though). This doesn't apply for return-type. [basic.scope.class]/4

The potential scope of a declaration that extends to or past the end of a class definition also extends to the regions defined by its member definitions, even if the members are defined lexically outside the class (this includes static data member definitions, nested class definitions, and member function definitions, including the member function body and any portion of the declarator part of such definitions which follows the declarator-id, including a parameter-declaration-clause and any default arguments).

like image 66
songyuanyao Avatar answered Jan 10 '23 06:01

songyuanyao