No overloaded operator<< for right-hand float?

I know that's not true, because I've used it a few hundred times, but that's the error I'm getting.
error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'float' (or there is no acceptable conversion)


In the following line of code
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
   #include <iostream>
   #include <fstream>
   #include <string>
   #include <sstream>
   struct Vector
   {
      float v[4];

      Vector();
      float operator[](int pos) const;
   /*...*/
   };
   float Vector::operator[](int pos) const
   {
      return v[pos];
   }

/*...*/

   Vector eye, at, up;
   /*...*/
   std::fstream f(filename,std::ios::in);
   std::string str,trash;
   std::stringstream _str(str,std::ios::in);
   f >> str;
   if(str == "CAMERA" && f >> eye[0] >> eye[1] >> eye[2] >> at[0] >> at[1] >> at[2] >> up[0] >> up[1] >> up[2] >> fovy >> nearp >> farp >> screenWidth >> screenHeight)
   {
      /*...*/
   }


More specifically, in the if-statement.
You need to return a reference.
What, with the operator? I don't think so. I want to get access to the value, not its address.

Isn't what I did technically equivalent to cin >> (float) a? Shouldn't that work?
No, because the value returned is temporary and will not affect the actual value itself.

There is a difference between a reference variable and a memory address.
also note that if you're returning a non-const reference, you really should make the function nonconst.

I would make two [] operators to be const correct:

1
2
3
4
5
6
7
8
9
   float Vector::operator[](int pos) const // const function, returns float
   {
      return v[pos];
   }

   float& Vector::operator[](int pos) // nonconst function, returns float&
   {
      return v[pos];
   }
Topic archived. No new replies allowed.