Ross Bauer
Ross Bauer

Reputation: 13

Class template instantiation error: type not declared in this scope

So I am writing a particle accelerator code for a c++ class, and Im having trouble with the initial class set up Im being asked to use. The professor wants us to use templates for the class, but when I try to implement them, I receive a number of errors. For instance:

#include <iostream>
#include <cmath>

using namespace std;
template <class Type>
class ThreeVector {
private:
    Type mx;
    Type my;
    Type mz;
public:
    ThreeVector();
    ThreeVector(Type x=0, Type y=0, Type z=0);


};

ThreeVector<Type>::ThreeVector() {
    Type mx=0;
    Type my=0;
    Type mz=0;
}
ThreeVector<T>::ThreeVector(Type x, Type y, Type z) {
    mx=x;
    my=y;
    mz=z;
}

is part of my header file (the class is required to be in a header file). When I run my program (can provide if needed), it gives me the following errors:

ThreeVector.h:30:13: error: ‘Type’ was not declared in this scope

ThreeVector.h:30:17: error: template argument 1 is invalid

ThreeVector.h: In function ‘int ThreeVector()’: ThreeVector.h:30:32: error: ‘int ThreeVector()’ redeclared as different kind of symbol

ThreeVector.h:6:7: note: previous declaration ‘template class ThreeVector’

ThreeVector.h:31:2: error: ‘Type’ was not declared in this scope

ThreeVector.h:32:7: error: expected ‘;’ before ‘my’

ThreeVector.h:33:7: error: expected ‘;’ before ‘mz’

ThreeVector.h: At global scope: ThreeVector.h:35:13: error: ‘Type’ was not declared in this scope

These problems did not exist before I started using the template, i.e., if I explicitly define all variable types, my class works fine. So, Im not really sure what is wrong with my template definition? If anyone could help, Id be really appreciative.

Thanks!

Upvotes: 1

Views: 7544

Answers (2)

R Sahu
R Sahu

Reputation: 206557

  1. You forgot to add

    template <typename Type>
    

    Before the function definition.

  2. Remove Type from the body of the function. When you add Type, you are declaring three function variables that are not related to the class members.

template <typename Type>
ThreeVector<Type>::ThreeVector() {
    mx=0;
    my=0;
    mz=0;
}

You can make it better by initializing the member using the initializer list syntax:

template <typename Type>
ThreeVector<Type>::ThreeVector() : mx(0), my(0), mz(0) { }

Upvotes: 1

phantom
phantom

Reputation: 3342

You have to declare template arguments for methods that are part of a template class and yet are defined outside of the class definition. So to make your function definitions you have to do this:

template<class Type>
ThreeVector<Type>::ThreeVector() {
    mx=0;
    my=0;
    mz=0;
}

template<class Type>
ThreeVector<T>::ThreeVector(Type x, Type y, Type z) {
    mx=x;
    my=y;
    mz=z;
}

This is because the templated type is not available to the method declarations when they are outside of the class definition. To fix this you have to add a template< ... > with the same arguments as for the original class.

Also, you should use initializer lists to initialize members. That would make your constructors look like this:

template<class Type>
ThreeVector<Type>::ThreeVector() : 
    mx(0),
    my(0),
    mz(0)
{

}

Upvotes: 2

Related Questions