operator Overload Getting weired error

In my class i have :
friend ostream& operator<<(ostream& output,vector<T>& );

this function was declared as following:
ostream& operator<<(ostream& output,vector<T>& vec)
{
output<< "("<<vec.x <<","<<vec.y<<","<<vec.z<<")";
return output;

}

This is the error i am getting:
1>main.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class

I hope someone can help me , i am new to the operator overload but i understand how it works.

Thanks
This compiles fine ; are you missing template <class T> ?

template<class T>
ostream& operator<<(ostream& output,vector<T>& vec)
{
output<< "("<<vec.x <<","<<vec.y<<","<<vec.z<<")";
return output;
}


Assuming you are using a custom datatype called vector and not the library vector.
Yah i do have that part. but for some reason its not working , i hate errors like this lol.
Thanks for the reply tho.
Can you post the complete code ? Is iostream included ?
no its not , is it needed ?
this is my header file;

#include <iostream>
using namespace std;


template <class T> class vector{

public:

vector(T X1,T X2,T X3){x=X1;y=X2;z=X3;};
friend ostream& operator<<(ostream& output,vector<T>& );
T plus(T a);
//private:

T x,y,z;


};
template <class T>
ostream& operator<<(ostream& output,vector<T>& vec)
{
output<< "("<<vec.x <<","<<vec.y<<","<<vec.z<<")";
return output;

}

i am still in the begining of my project , so theres noting yet interesting there.
in my main i only declaring a type vector a and output it on the screen for now
no its not , is it needed ?
this is my header file;

#include <iostream>
using namespace std;


template <class T> class vector{

public:

vector(T X1,T X2,T X3){x=X1;y=X2;z=X3;};
friend ostream& operator<<(ostream& output,vector<T>& );
T plus(T a);
//private:

T x,y,z;


};
template <class T>
ostream& operator<<(ostream& output,vector<T>& vec)
{
output<< "("<<vec.x <<","<<vec.y<<","<<vec.z<<")";
return output;

}

i am still in the begining of my project , so theres noting yet interesting there.
in my main i only declaring a type vector a and output it on the screen for now
Yep, http://www.cplusplus.com/reference/iostream/
And looks like you already have it.


Also this is relevant :
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
using namespace std;

template <class T>
class vector;

template <class T>
ostream& operator<< (ostream& output,vector<T>& );

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

template <class T>
ostream& operator<< (ostream& output,vector<T>& vec)
{
    output<< "("<<vec.x <<","<<vec.y<<","<<vec.z<<")";
    return output;
}

int main()
{
    vector<int> v1(10,10,10);
    cout<<v1;
}

Last edited on
Hi,
Thank you very much for your help,
I still dont see why ur version compile vs my version that is using header file
My bad! I had just compiled the overloaded part :)

Also remember to instantiate the template for better error check.

Cheers!
ok that works now :),
so it seems like i was missing <> in my operator function , what does that thing do??
Topic archived. No new replies allowed.