Jun 12, 2010 at 4:11pm UTC
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
Jun 12, 2010 at 4:20pm UTC
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.
Jun 12, 2010 at 4:26pm UTC
Yah i do have that part. but for some reason its not working , i hate errors like this lol.
Thanks for the reply tho.
Jun 12, 2010 at 4:28pm UTC
Can you post the complete code ? Is iostream included ?
Jun 12, 2010 at 4:31pm UTC
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
Jun 12, 2010 at 4:33pm UTC
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
Jun 12, 2010 at 4:51pm UTC
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 Jun 12, 2010 at 5:03pm UTC
Jun 12, 2010 at 5:12pm UTC
Hi,
Thank you very much for your help,
I still dont see why ur version compile vs my version that is using header file
Jun 12, 2010 at 5:18pm UTC
My bad! I had just compiled the overloaded part :)
Also remember to instantiate the template for better error check.
Cheers!
Jun 12, 2010 at 5:38pm UTC
ok that works now :),
so it seems like i was missing <> in my operator function , what does that thing do??