Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding the operator >> to my class

Status
Not open for further replies.

Kudjat

Programmer
Jul 7, 2003
8
FR
Hi everyone, I'm not a C++ guru and so would need a little help please !

For 3D matters, I created a class vertex3D:

class vertex3D
{
public:
float verX; // x coordinate
float verY; // huh :)
float verZ;

...

As I save the coordinates in a file, I added the operator << to my class:

ostream & operator << (ostream &o, const vertex3D &v)
{
return o << v.verX << " " << v.verY << " " << v.verZ << endl;
}

and use the following code in the main function:


vertex3D vertex_Array[nb_vertices];
...
ofstream recordfile;
for (i=0; i< nb_vertices; i++) recordfile << vertex_Array;
recordfile.close();


All this works well, but now I'd like to read the coordinates from the file via the >> operator, and this is where I'm stuck (told you I'm not a guru !).


I tried to add the function:

friend ifstream & operator >> (ifstream &, vertex3D &);

istream & operator >> (istream &i, vertex3D &v)
{
return i >> v.verX >> v.verY >> v.verZ;
}


VC++ compiles the code without indicating any error, but if I try:


for (int i=0; i< nb_vertices; i++) I >> vertex_Array; // read the coordinates from file


I get:

Linking...
main.obj : error LNK2001: unresolved external symbol "class ifstream & __cdecl operator>>(class ifstream &,class vertex3D &)" (??5@YAAAVifstream@@AAV0@AAVvertex3D@@@Z)
Debug/Tree.exe : fatal error LNK1120: 1 unresolved externals


The former code I used in the main function worked well:

for (int i=0; i< nb_vertices; i++)
I >> vertex_Array.verX >> vertex_Array.verY >> vertex_Array.verZ;


Anyone can help me please ??? :)
 
Look at your declaration:

friend ifstream & operator >> (ifstream &, vertex3D &);

Your implementation uses istream instead of ifstream:

istream & operator >> (istream &i, vertex3D &v)
{
return i >> v.verX >> v.verY >> v.verZ;
}

Try changing it to ifstream and see if that works.
 
thanks !

I just found the same error and it works :)

I tried first with the istream, then tried with the ifstream to see if it would work, but forgot to re-declare correctly the function definition.

sorry for my mistake and thanks again ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top