Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何正确过载'+';结构的运算符_C++_Operator Overloading - Fatal编程技术网

C++ 如何正确过载'+';结构的运算符

C++ 如何正确过载'+';结构的运算符,c++,operator-overloading,C++,Operator Overloading,我想为struct重载'+'运算符,但收到编译器警告 以下是我的尝试: 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.

我想为
struct
重载'+'运算符,但收到编译器警告 以下是我的尝试:

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;
};
编译器警告:

[警告]非静态数据成员初始值设定项仅适用于12行中的-std=c++11或-std=gnu++11[默认启用]


警告告诉您有关线路的信息:

int y=0;
在C++11之前,非静态非常量成员上不能有初始化器。如果要将
y
初始化为0,则必须为
wektor
提供一个具有成员初始化列表的构造函数

尽管如此,您的
运算符+
参数的类型应为
const-wektor&
。它还应该按值返回,因为此时您正在返回对本地对象的引用,该对象将在函数结束时被销毁,这是不好的。应该是这样的:

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;
};

不是那样的。签名应该是

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

也就是说,不要通过引用从
+
运算符返回,更重要的是,不要通过引用返回临时值。

首先,二进制运算符+应该返回一个新值,而不是引用。如果以引用作为输入来实现,则应为常量:

wektor operator +(const wektor &a, const wektor &b);
其次,警告与此初始化有关:

struct wektor{
    int x;
    int y=0;    // HERE! C++11 only
    int norm(){
        return x*x+y*y;
    }
};
您只能在C++11中执行此操作。您可以在C++03中使用构造函数

struct wektor{
    wector() : y() {} // zero-initializes y
    int x;
    int y;
    int norm(){ return x*x+y*y;}
};
回到
操作符+
,我将实现一个成员
操作符+=
,然后在非成员
操作符+
中使用它:

wektor operator +(wektor a, const wektor &b)
{
  return a+= b;
}
或者,为
x
y
提供一个双参数构造函数:

wector(int x, int y) : x(x), y(y) {}
蚂蚁呢

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

这是警告,你使用的是C++ 11的一个特性,在以前的C++标准中是不可用的。 当你知道你的程序是按照你的想法工作的时候,你可以 通过执行以下操作来消除此错误:

如果您使用的是代码块:

  • 右键单击“生成选项…”
  • 选择“其他选项”选项卡
  • 添加“-std=gnu++11”
  • 如果您正在使用命令行:
    将“-std=gnu++11”添加到命令arg中。

    是struct
    wektor{
    第0行吗?为什么不阅读错误消息?wektor是否应该
    vector
    ?为什么
    norm()
    计算震级平方,而不是与标准化有任何关系?哦,我以前只听说它是一个发音错误,但现在我明白了,至少极点是这样拼写的。我的错。我推断它是一个数学向量,但当
    norm
    与数学向量标准化无关时,我感到困惑。@moing Duck键入“wektor”并按play键以听到波兰语的声音:)而不使用
    y=0
    我得到:“[警告]返回了对局部变量“c”的引用[默认启用]”@Qbik执行这两个更改-这是&不通过引用返回。@Qbik您不应该返回对局部变量的引用。局部变量超出范围并被销毁。因此,引用处于悬空状态。真的吗?WeeeeId。那么,他们为什么要处理警告?为什么需要启用“C++11”?在g++上,对于4.7以前的版本,
    -std=c++0x
    ,以及此后的
    -std=c++11
    -std=c++0x
    )。对于其他编译器,我不知道。你的答案很好,你能告诉我如何重载
    +
    以使验证操作
    w+5
    ,其中
    w
    wektor
    ?@Qbik要么提供
    wector操作符+
    ,它以
    wector
    int
    作为参数(但是
    5+w
    不起作用)或为
    wector
    提供一个(隐式)转换构造函数,该构造函数将
    int
    作为参数:
    wector(intx):x(x),y(0){}