Jaanus
Jaanus

Reputation: 16561

What's the cause of the error with this C++ template code?

This code results in a compile-time error:

#include <iostream>
#include <vector>
#include <cmath>

template <unsigned short n>
class Vector {
    public:
        std::vector<float> coords;

        Vector();
        Vector(std::vector<float> crds);


        float distanceFrom(Vector<n> v);

        template <unsigned short m>
        friend std::ostream& operator <<(std::ostream& out, const Vector<m>& v);
};



    template <unsigned short n>
Vector<n>(vector<float> crds) {  // HERE IS ERRRO

}

Compile error:

C:\CodeBlocks\kool\praks3\vector.h|29|error: expected ')' before '<' token|
||=== Build finished: 1 errors, 0 warnings ===|

Upvotes: 1

Views: 179

Answers (5)

Erik
Erik

Reputation: 91320

template <unsigned short n>
Vector<n>::Vector(vector<float> crds) {
}

EDIT: As others mentioned, if you're not using namespace std; you also need std::vector<float>

Upvotes: 4

Goz
Goz

Reputation: 62333

Should it be this?

template <> Vector<unsigned short>::Vector(Vector<float> crds) 
{
    // BLAH
}

It looks to me like you've got confused on template specialisation.

Upvotes: 0

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361802

Here is how you should define the constructor outside the class:

template <unsigned short n>
Vector<n>::Vector(std::vector<float> crds) {
//also notice this ^^^^                 
}

Upvotes: 8

deek0146
deek0146

Reputation: 1004

Vector::Vector(constructor args) You just forgot to scope the constructor

Upvotes: 0

Frizi
Frizi

Reputation: 2940

you probably miss std:: prefix in your vector<float>.

Upvotes: 0

Related Questions