Overloading << operator for a Vector3D

Hello I'm trying to overload a << and >> for a Vector3D class and I'm completely lost on how to do it. If anyone can help me it's be greatly appreciated. Here is what I have so far:

In Header File
1
2
3
 friend ostream& operator<< ( ostream& ostr, const Vector3D& myVector );

   friend istream& operator>> ( istream& istr, Vector3D& myVector );


In .cpp file
1
2
3
4
5
6
7
8
9
10
11
12
ostream& operator << (ostream& ostr, const Vector3D& myVector)
{

    ostr << Vector3D.x << ' '<< Vector3D.y << ' '<< Vector3D.z;
    return ostr;
}

istream& operator >> (istream& istr, const Vector3D& myVector)
{
    istr >> Vector3D.x >> Vector3D.y >> Vector3D.z;
    return istr;
}


I'm just not sure how to make this work. Once again thank you for any help.
The .cpp file has been fixed to read this:

1
2
3
4
5
6
7
8
9
10
11
12
ostream& operator << (ostream& ostr, const Vector3D& myVector)
{

    ostr << myVector.x << ' '<< myVector.y << ' '<< myVector.z;
    return ostr;
}

istream& operator >> (istream& istr, const Vector3D& myVector)
{
    istr >> myVector.x >> myVector.y >> myVector.z;
    return istr;
}


but I'm getting all sorts of errors like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Vector3D.cpp:130: error: no match for ?operator>>? in ?istr >> myVector->Vector3D::x?
/usr/include/c++/4.3/istream:123: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:127: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:134: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:170: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:174: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:177: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:181: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:184: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:188: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:192: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:197: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:201: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:206: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:210: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:214: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:218: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:242: note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
Vector3D.cpp:128: note:                 std::istream& operator>>(std::istream&, const Vector3D&)
Try removing the const:
1
2
3
4
istream& operator >> (istream& istr, Vector3D& myVector){
    istr >> myVector.x >> myVector.y >> myVector.z;
    return istr;
}

You don't want it const if you are loading new values.
Wow thanks a ton. That really did away with all that stuff. Now the only issue I'm having is that I'm getting an error saying:

 
Vector3D.cpp:7: error: expected unqualified-id before ?using?


It's pointing to the using namespace std; in this code:

1
2
3
4
5
#include "Vector3D.h"
#include <iostream>
#include <cmath>
using namespace std;


Anyone know what I can do to remedy this?
the error is in a header... i quess it is in Vector3D.h
I checked that out but I'm not sure what the problem is. Here is that Header file and it's the only one:

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
35
#include <iostream>
#include <cmath>
using namespace std;


class Vector3D
{
  private:
      

  public:
   
   float x,y,z;
   Vector3D Vector3d (float xVector= 0.0, float yVector =0.0, float zVector = 0.0);
   void Read();
   void Print() const;
   const bool operator == (const Vector3D rhs) const;
   const Vector3D operator * (const Vector3D rhs) const;
   const bool operator != (const Vector3D rhs) const;
   const Vector3D operator + (const Vector3D rhs) const;
   const Vector3D operator - (const Vector3D rhs) const;
   const float operator % (const Vector3D rhs) const;
   const Vector3D operator = (const Vector3D rhs) const;
   const Vector3D operator * (const int rhs) const;
   

   float magnitude (Vector3D Vector);

   float normalize (Vector3D Vector);


   friend ostream& operator<< ( ostream& ostr, const Vector3D& myVector );

   friend istream& operator>> ( istream& istr, Vector3D& myVector );
}


All the consts are supposed to be there as well due to the assignment.
Last edited on
Put at semi-colon at the end of the closing bracket of the class.
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
35
36
37
38
#include <iostream>
#include <cmath>
using namespace std;


class Vector3D
{
  private:
      

  public:
   
   float x,y,z;
   Vector3D Vector3d (float xVector= 0.0, float yVector =0.0, float zVector = 0.0);
   void Read();
   void Print() const;
   const bool operator == (const Vector3D rhs) const;
   const Vector3D operator * (const Vector3D rhs) const;
   const bool operator != (const Vector3D rhs) const;
   const Vector3D operator + (const Vector3D rhs) const;
   const Vector3D operator - (const Vector3D rhs) const;
   const float operator % (const Vector3D rhs) const;
   const Vector3D operator = (const Vector3D rhs) const;
   const Vector3D operator * (const int rhs) const;
   

   float magnitude (Vector3D Vector);

   float normalize (Vector3D Vector);


   friend ostream& operator<< ( ostream& ostr, const Vector3D& myVector );

   friend istream& operator>> ( istream& istr, Vector3D& myVector );
    
      //----------------------------------
};    //  Add the ;                            <---------------- LOOK!
      //----------------------------------  
Topic archived. No new replies allowed.