-2

The detailed discussion of the problem is shown in this link. I am trying to sum up two instance variable defined inside class Point and assign it to a different variable temp.

class Point{
    public:
      double x;  
      double y;       
      friend istream& operator>>(istream& input, Point& p);
      double operator=(Point& p);      
      double getSqX(void);      
      double getSqY(void);
      double LengthSquared(void);  


    };    
      double Point::getSqX(void){
          return pow(x,2);}

      double Point::getSqY(void){
          return pow(y,2);}

       double Point::LengthSquared(){ return getSqX() + getSqY(); }


    istream& operator>>(istream& input, Point& p){
     ... // over load the >> operator      
      return input;
    };


     int main(){
        double temp;        
        vector<vector<Point> > FFTfile= some function that loads data();        
        for (int i = 0; i < FFTfile.size(); i++){
            for (int j = 0; j < FFTfile[i].size(); j++){
                 temp=FFTfile[j].LengthSquared();            
            }           

        }       
        return(0);
}

Edit:
Based on the suggestions, I created a method LengthSquared(), but I still get following error:

 error: 'class std::vector<Point>' has no member named 'LengthSquared'  temp=FFTfile[j].LengthSquared();
Spandyie
  • 914
  • 2
  • 11
  • 23
  • Hi, check out the answers from Peer and Martin, also for clarity purpose the assignment operator in c++ looks like this `class_name & class_name :: operator= ( class_name )` (SRC: http://en.cppreference.com/w/cpp/language/copy_assignment). I'm guessing that you just mixed your approach. This should look like this "`Point Point::operator=(Point& p)`" or "`double Point::operator=(Point& p)`" - not recommended. – Guillotine Sep 01 '17 at 07:18
  • I think that's the most abusive operator abuse I've seen. You would need to to write `Point p; Point p2; double x = p = p2;` to get the sum of `p2`'s squares into `x`, and you would be unable to assign one `Point` to another. I don't think you actually want to do this. – molbdnilo Sep 01 '17 at 07:22
  • @Spandy why would you even want to do this? Overloading the assignment operator to do something that has absolutely nothing to with assignments is just making your code needlessly unreadable without gaining anything. If you really really want this to be an operator, at least use a different one than `operator=`. – Zinki Sep 01 '17 at 08:06
  • If what you actually want is `Point p; double d = p;`, you can create a conversion operator. It's still seriously confusing, though. – molbdnilo Sep 01 '17 at 08:18

2 Answers2

1

You should never overload the assignment operator this way. Someone reading your code will be confused, since assignment normally means .. assign value to object.

Instead, create a method like this

double Point::LengthSquared() { return getSqX() + getSqY(); }
Peer Sommerlund
  • 502
  • 6
  • 14
0

an assignment-operator should have the following interface:

Point& operator=(const Point& other);

or

Point& operator=(const AnotherType& other);

to allow assignments of other types.

You are abusing the assignment operator. Use a regular method.

Martin Fehrs
  • 792
  • 4
  • 13
  • How would returning object of type Point help though? Would I be able to assign the output from "operation=" to a float variable ? – Spandyie Sep 01 '17 at 07:30
  • 1
    The point is, that you should not use the assignment operator for this. Create a regular Method. Peer gave you an example.LengthSquared() – Martin Fehrs Sep 01 '17 at 07:37
  • 1
    @Spandy how would that even look (assigning the output of `operator=` to a float). Do you want it Like this: `float f = pointA = pointB` ? Because don't do that, its completely unreadable and confusing. Also, you can't overload `=` like that, use a different operator if you absolutely must. – Zinki Sep 01 '17 at 08:10