重载+运算符

wol*_*k88 3 c++ operator-overloading

我正在尝试熟悉C++中的运算符.我想我会用一个简单的矢量加法案例来做到这一点.不幸的是,我似乎遇到了一些问题.我的班级定义如下:

#ifndef _MVEC_H_
#define _MVEC_H_

#include "Error.h" //I define things like throw(message) here, it works and is not the issue

class MVec {
        private:
                double vec[3];
        public:
                MVec();
                MVec(double &);
                MVec(double *);
                MVec(MVec &);
                MVec & operator=(MVec &);
                inline double & operator[](const int i);
                inline const double & operator[](const int i) const;
                MVec operator+(const MVec &) const;
                ~MVec();
};

MVec::MVec() {}

MVec::MVec(double &a) {
        for(int i = 0; i < 3; i++)
                vec[i] = a;
}

MVec::MVec(double *a) {
        for(int i = 0; i < 3; i++)
                vec[i] = *a++;
}

MVec::MVec(MVec &rhs) {
        for(int i = 0; i < 3; i++)
                vec[i] = rhs[i];
}

MVec & MVec::operator=(MVec &rhs) {
        if(this != &rhs)
                for(int i = 0; i < 3; i++)
                        vec[i] = rhs[i];

        return *this;
}

inline double & MVec::operator[](const int i) {
        #ifdef _CHECKBOUNDS_
        if(i < 0 || i >= 3)
                throw("Subscript out of bounds");
        #endif

        return vec[i];
}

inline const double & MVec::operator[](const int i) const {
        #ifdef _CHECKBOUNDS_
        if(i < 0 || i >= 3)
                throw("Subscript out of bounds");
        #endif

        return vec[i];
}

MVec MVec::operator+(const MVec &vec1) const {
        MVec ans;

        for(int i = 0; i < 3; i++)
                ans[i] = vec[i] + vec1[i];

        return ans;
}

MVec::~MVec() {
        delete[] vec;
}

#endif
Run Code Online (Sandbox Code Playgroud)

[]运算符似乎按预期工作.不幸的是,向量加法运算符没有.具体来说,当我运行代码时:

#include "Error.h"
#include "MVec.h"
#include <cstdlib>
#include <iostream>

int main(int argc, char *argv[]) {
        MVec a, b, c;
        a[0] = 1; a[1] = 2; a[2] = 3;
        b[0] = 5.9906; b[1] = 72.1139; b[2] = 83.1324;

        //c = a + b;

        std::cout << (a + b)[0] << std::endl;
        std::cout << (a + b)[1] << std::endl;
        std::cout << (a + b)[2] << std::endl;

        exit(0);
}
Run Code Online (Sandbox Code Playgroud)

当我取消注释行c = a + b; 我收到编译器错误:

在'c = MVec :: operator +(const MVec&)const(((const MVec&)((const MVec*)(&b))))'''''''''''''''''''''''''''

当我评论它时,我在第一个std :: cout之后得到一个glibc检测到的错误.据推测,我在运算符+函数中创建的临时变量出错了.不幸的是,我不够(非常)足够聪明地弄清楚是什么.对此的任何和所有见解都会非常有帮助.

jua*_*nza 8

您需要在复制构造函数中const引用MVec,以便能够与临时工具一起使用:

MVec(const MVec &);
Run Code Online (Sandbox Code Playgroud)

这同样适用于赋值运算符,构造函数采用double:

MVec(const double &); // or no reference, MVec(double);
MVec& operator=(const MVec& rhs);
Run Code Online (Sandbox Code Playgroud)

您还应该delete [] vec从析构函数中删除它,因为vec它不是动态分配的.这可能是glibc错误的原因.

现在,为了表达如

SomeVec = 1.0 + SomeOtherVec;
Run Code Online (Sandbox Code Playgroud)

你需要声明operator+作为非成员的fumction:

MVec operator+(const MVec& lhs, const MVec& lhs); 
Run Code Online (Sandbox Code Playgroud)

这将允许隐式转换在LHS和RHS中.通常,将这些类型的运算符作为非成员函数是一个好主意,以保证LHS和RHS操作数之间的对称性.

另一方面,根本不允许隐式转换可能更有意义double.您可以通过创建相关的构造函数来实现此目的explicit:

explicit MVec(double);
Run Code Online (Sandbox Code Playgroud)