SOLVED: See answer on the bottom
I'm rather new to C++ so this might be a stupid question, but I am trying to write a template class for vectors using the built in STL vectors. It is an assignment from the University. I created a Header file and a cpp-source file, so I can define the class Vector in the Header and declare it's functions in the source file. I get the following errors:
Vector.cpp:27:59: error: invalid use of incomplete type ‘class Vector<T, n>’
Vector<T, n>& Vector<T, n>::operator=(const Vector<U,n>& v){
^
In file included from Vector.cpp:1:0:
Vector.h:5:7: note: declaration of ‘class Vector<T, n>’
class Vector {
^~~~~~
Vector.cpp:42:59: error: invalid use of incomplete type ‘class Vector<T, n>’
Vector<T, n>& Vector<T, n>::operator+(const Vector<U,n>& v){
^
In file included from Vector.cpp:1:0:
Vector.h:5:7: note: declaration of ‘class Vector<T, n>’
class Vector {
^~~~~~
My code is this:
Header file (Vector.h):
#include <vector>
using namespace std;
template <class T, int n>
class Vector {
public:
Vector();
Vector(int);
Vector(const Vector<T, n>&);
~Vector();
template<class U>
Vector<T, n> operator=(const Vector<U, n>&);
T& operator[](int i);
template<class U>
Vector<T, n> operator+(const Vector<U, n>&);
template<class U>
Vector<T, n> operator-(const Vector<U, n>&);
template<class U>
T operator*(const Vector<U, n>&);
template<class S>
Vector<T, n> operator*(const S&);
/* template<class S>
friend Vector<T, n> operator*(const S&, Vector<T, n>&);*/
template<class F>
Vector<T, n> apply_Funktor();
private:
vector<T> values;
const int size;
};
/*
template<class S, class T, int n>
Vector<T, n> operator*(const S& s, const Vector<T, n> v){
return v * s;
};*/
And the source-cpp file:
#include "Vector.h"
#include <vector>
using namespace std;
template <class T, int n>
Vector<T, n>::Vector(): size(n){
vector<T> values;
}
template <class T, int n>
Vector<T, n>::Vector(int v): size(n){
vector<T> values;
values.assign(v, n);
}
template <class T, int n>
Vector<T, n>::Vector(const Vector<T, n>& v): size(v.size){
values = v.values;
}
template <class T, int n>
Vector<T, n>::~Vector(){
delete values;
}
template<class U, class T, int n>
Vector<T, n>& Vector<T, n>::operator=(const Vector<U,n>& v){
delete values;
vector<T> values;
for(int i = 0; i < size; i++){
values.push_back((T) v.values[i]);
}
return *this;
}
template<class T, int n>
T& Vector<T, n>::operator[](int i){
return values[i];
}
template<class U, class T, int n>
Vector<T, n>& Vector<T, n>::operator+(const Vector<U,n>& v){
//more Code to come but I already get the error here
}
The error occurs when I try to build my project with NetBeans. I dont have a main yet to use the Vector. I already searched the internet for solutions but I didnt come across anything helpful. I learned about forward declaration but it seems to me that this isnt the case here, because Im including the header file.
Thank you for your help :)
!!!EDIT / SOLUTION: My tutor in class solved it: The problem is that instead of doing
template<class U, class T, int n>
I have to put it like this:
template<class T, int n>
template<class U>