Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: "error: expected class-name before ‘{’ token" when inheriting a template class

I've looked around for a solution to my problem and found lots about cyclic references and namespace issues (neither apply in my case), but nothing like the problem I'm having.

I have a template class defined and implemented in maths/matrix.h:

template<class T>
class Matrix
{
public:
    // constructors, destructors and what not...
};

I have another template class defined and implemented in maths/vector.h

#include <maths/matrix.h>

template<class T>
class Vector : public Matrix
{
public:
    // constructors, destructors and what not...
};

I get this error "expected class-name before ‘{’ token" in vector.h which is really bugging me. It's not anything to do with matrix.h and vector.h being in a maths sub-folder because I can use matrix.h in other parts of my application without any problems. I think it has something to do with Matrix being a templated class because when I make Vector a subclass of a non-templated class (SomeClass.h for example) everything compiles ok.

Many thanks to anyone that can help :)

like image 287
The Sockmonster Avatar asked Apr 17 '11 14:04

The Sockmonster


2 Answers

You need to inherit from the concrete class, i.e. from Matrix<T>, not merely Matrix:

template<class T>
class Vector : public Matrix<T>
{
    …
};
like image 151
Konrad Rudolph Avatar answered Nov 10 '22 10:11

Konrad Rudolph


You're missing two things.

template<typename T>
class Vector : public Matrix <T> //<----- first : provide the type argument
{

}; //<-------- second : semi-colon (same from Matrix class also)
like image 6
Nawaz Avatar answered Nov 10 '22 09:11

Nawaz