The Sockmonster
The Sockmonster

Reputation: 189

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 :)

Upvotes: 9

Views: 6072

Answers (2)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361352

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)

Upvotes: 6

Konrad Rudolph
Konrad Rudolph

Reputation: 545568

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

template<class T>
class Vector : public Matrix<T>
{
    …
};

Upvotes: 13

Related Questions