I’m trying to familiarize myself with operators in C++. I figured I would do so with a simple case of vector addition. Unfortunately, I seem to have run into some issues. My class definition is as below:
#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
The [] operator appears to work as intended. Unfortunately, the vector addition operator does not. Specifically, when I run the code:
#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);
}
When I uncomment the line c = a + b; I get a compiler error:
no match for ‘operator=’ in ‘c = MVec::operator+(const MVec&)
const(((const MVec&)((const MVec*)(& b))))’
When I comment it out, I get a glibc detected error after the first std::cout. Presumably, I am doing something wrong with the temporary variable I am creating in the operator+ function. Unfortunately, I am not (quite) smart enough to figure out what. Any and all insight into this would be very helpful.
You need to take a
constreference toMVecin your copy constructor in order to be able to use it with temporaries:The same applies to the assignment operator, and the constructor taking a
double:You should also remove the
delete [] vecfrom the destructor, becausevecis not dynamically allocated. This is the likely cause of the glibc error.Now, in order for expressions such as
you need to declare the
operator+as a non-member fumction:This will allow for implicit conversions both on the LHS and on the RHS. In general it is a good idea to have these types of operators as non-member functions, to guarantee symmetry between LHS and RHS operands.
On the other hand, it probably makes more sense not to allow the implicit conversions from
doubleat all. You can achieve this by making the relevant constructorexplicit: