简体   繁体   中英

How to properly overload '+' operator for a struct

I'd like to overload the '+' operator for A struct but I'm getting compiler warning Here's my attempt :

struct wektor{
    int x;
    int y=0;    
    int norm(){
        return x*x+y*y;
    }
};

wektor& operator +(wektor &a,wektor &b){
    wektor c;
    c.x=a.x+b.x;  // 12 line - warning here
    c.y=a.y+b.y;
    return c;
};

Compiler warning:

[Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default] in 12 line

The warning is telling you about the line:

int y=0;

You can't have an initialiser on a non-static non-const member prior to C++11. If you want to initialise y to 0 then you have to provide a constructor for wektor with a member initialization list.

Nonetheless, your operator+ parameters should be of type const wektor& . It should also return by value, because at the moment you're returning a reference to a local object that will be destroyed at the end of the function, and that is bad. It should look like this:

wektor operator +(const wektor &a, const wektor &b){
    wektor c;
    c.x=a.x+b.x;  // 12 line - warning here
    c.y=a.y+b.y;
    return c;
};

First of all, binary operator+ should return a new value, not a reference. And if implemented in terms of references as input, these should be const:

wektor operator +(const wektor &a, const wektor &b);

Second, the warning is about this initialization:

struct wektor{
    int x;
    int y=0;    // HERE! C++11 only
    int norm(){
        return x*x+y*y;
    }
};

You can only do this in C++11. You could use a constructor in C++03.

struct wektor{
    wector() : y() {} // zero-initializes y
    int x;
    int y;
    int norm(){ return x*x+y*y;}
};

Going back to the operator+ , I would implement an member operator+= , and then use it in a non-member operator+ :

wektor operator +(wektor a, const wektor &b)
{
  return a+= b;
}

Alternatively, give wector a two parameter constructor for x and y :

wector(int x, int y) : x(x), y(y) {}

ant then

wektor operator + (const wektor& a, const wektor &b)
{
  return wector(a.x + b.x, a.y + b.y);
}

Not like that. The signature should be

wektor operator +(const wektor &a, const wektor &b)

Ie don't return by reference from the + operator, and, even more importantly, don't return a temporary by reference.

That's a warning that you're using a feature from C++11, which isn't available in previous C++ standards.

When you know that what you've programed works the way you think, you can get rid of this error by doing:

If you're using CodeBlocks:

  1. Right-Click "Build Options..."
  2. Select the "Other Options" tab
  3. Add "-std=gnu++11"

If you're using the command line: Add "-std=gnu++11" to the command arg's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM