Class T function is giving me errors .

Hello guys,
I have the following problem
in my class vector i have this function:
vector<T> plus(vector<T>&);
which is initialized as followed:
template <class T>
vector::vector<T> plus(vector<T>& vec){
vector<T> result;
result.x=this.x+vec.x;
result.y=this.y+vec.y;
result.z=this.z+vec.z;
return result;
}
when i call two parameters type class vector a and be
when i do this :
a.plus(b); i get the following :
1>main.obj : error LNK2019: unresolved external symbol "public: class vector<int> __thiscall vector<int>::plus(class vector<int> &)" (?plus@?$vector@H@@QAE?
Last edited on
You have to place function with templates inside the .h(pp) file, not the .cpp.
Yes this is where i have inside the ,h under the declaration of my class
closed account (1yR4jE8b)
Your function definition is in the global scope, it needs to be in the scope of the class it is being defined for and you should probably pass by const reference:

1
2
3
4
5
6
7
8
9
template <class T>
vector<T> vector<T>::plus(const vector<T>& vec)
{
     vector<T> result; 
     result.x=this.x+vec.x;
     result.y=this.y+vec.y;
     result.z=this.z+vec.z;
     return result;
}
yah you are right but vector class is the only one i have now , but anyways i tried that but didnt change the error:
1>main.obj : error LNK2019: unresolved external symbol "public: class vector<int> __thiscall vector<int>::plus(class vector<int> &)" (?plus@?$vector@H@@QAE?

I think theres a problem with passing class to a function is this is how it should be or am i doing something wrong
Can I see the .h file with the class in it?
#include <iostream>
using namespace std;

template <class T>
class vector
{
public:
vector(){x=0;y=0;z=0;};
vector(T X1,T X2,T X3){x=X1;y=X2;z=X3;};
friend ostream& operator<< <> (ostream& output,vector<T>& );

vector<T> plus(const vector<T>& vec);
//vector<T> minus(T&);
//vector<T> minus(T&);
//vector<T> minus(T&);
//vector<T> minus(T&);
//private:
T x,y,z;
};

template <class T>
ostream& operator<< (ostream& output,vector<T>& vec)
{
output<< "("<<vec.x <<","<<vec.y<<","<<vec.z<<")";
return output;
}
template <class T>
vector<T> vector<T>::plus(const vector<T>& vec)
{
vector<T> result;
result.x=this.x+vec.x;
result.y=this.y+vec.y;
result.z=this.z+vec.z;
return result;
}
if i make my plus function like this for example;
template <class T>
vector<T>::plus(const vector<T>& vec)
{
vector<T> result;
result.x=vec.x;
result.y=vec.y;
result.z=vec.z;
return result;
}
and call plus(a) it works , i really dont know
ok i got it working , i was not suppse to use this.

Thanks alot
Topic archived. No new replies allowed.