error: no match for 'operator='

Hi,

I'm having a problem with this error message.

To summarise the code I have done the following:

1) In a Point class I have an overloaded operator =

2) I've then created a Vector class that inherits from Point, I've also created further overloaded operators for + and -

3) In main() I've then created two Vectors, added them together and output the result:

Vector vect1(1, 2, 3);
Vector vect2(5, 4, 2);

Vector vect3;

vect3 = vect2 - vect1;

cout << vect3 << endl;

The full code I have posted to:

http://codepad.org/g1J7eFcs

There I have output vect2 rather than vect3 but this is irrelevant

My issue is that in Microsoft Visual C++ this code works, I however am using Code::Blocks 8.02 and both Code::Blocks and codepad.org throw this error.

Would someone be able to explain why and what the 'get-around' is.

Thanks in advance!


Post code please.
#include <cstdlib>
#include <iostream>

using namespace std;

class Point
{
public:
Point(float f_x = 0.0, float f_y = 0.0, float f_z = 0.0);
~Point();

void setXYZ(float X, float Y, float Z);
void setX(float X);
void setY(float Y);
void setZ(float Z);

void getXYZ(float &X, float &Y, float &Z);
float getX();
float getY();
float getZ();

Point operator =(Point &p);
private:
float x, y, z;
protected:

};

Point::~Point()
{
cout << "We're in the destructor " << (int)this << endl;
}

Point::Point(float f_x, float f_y, float f_z)
{
cout << "We're in the constructor with arguments " << (int)this << endl;
x = f_x;
y = f_y;
z = f_z;
}

void Point::setXYZ(float X, float Y, float Z)
{
setX(X);
setY(Y);
setZ(Z);
}

void Point::setX(float X)
{
x = X;
}

void Point::setY(float Y)
{
y = Y;
}

void Point::setZ(float Z)
{
z = Z;
}


void Point::getXYZ(float &X, float &Y, float &Z)
{
X = getX();
Y = getY();
Z = getZ();
}

float Point::getX()
{
return x;
}

float Point::getY()
{
return y;
}

float Point::getZ()
{
return z;
}

Point Point::operator =(Point &p)
{
setX(p.getX());
setY(p.getY());
setZ(p.getZ());

return *this;
}

ostream &operator << (ostream &stream, Point &p)
{
stream << p.getX() << " " << p.getY() << " " << p.getZ();
return stream;
}

istream &operator >> (istream &stream, Point &p)
{
float x, y, z;
stream >> x >> y >> z;
p.setXYZ(x, y, z);
return stream;
}
// -------------------------------
// Point class end
// -------------------------------

class Vector : public Point
{
public:
Vector (float X = 0.0, float Y = 0.0, float Z = 0.0);
Vector operator =(Vector &p);
Vector operator +(Vector &p);
Vector operator -(Vector &p);
// Vector operator =(Vector &p);
private:
protected:
};

Vector::Vector(float X, float Y, float Z) : Point(X, Y, Z)
{
}

Vector Vector::operator +(Vector &p)
{
Vector outV;
outV.setX(getX() + p.getX());
outV.setY(getY() + p.getY());
outV.setZ(getZ() + p.getZ());
return outV;
}

Vector Vector::operator -(Vector &p)
{
Vector outV;
outV.setX(getX() - p.getX());
outV.setY(getY() - p.getY());
outV.setZ(getZ() - p.getZ());
return outV;
}

Vector Vector::operator = (Vector &p)
{
setX(p.getX());
setY(p.getY());
setZ(p.getZ());

return *this;
}

int main()
{
/* float x, y, z;

Point myLocation(54, 76, 38);
Point franksLocation(1,5,8);

myLocation = franksLocation;

cout << myLocation << endl;
cin >> myLocation;
cout << (myLocation) << endl;
*/
Vector vect1(1, 2, 3);
Vector vect2(5, 4, 2);

Vector vect3;

vect3 = vect2 - vect1;

cout << vect3 << endl;

system("PAUSE");
return EXIT_SUCCESS;
}
There are too many bad habit in your code, I suggest you read the book "Effective C++" first.

The = operator should defined that:
1) the argument must be a const refference
2) return a refference of the class

The other operators you defined are also not right
and the constructors are not effective
Last edited on
We have encountered a very similar problem to this before.
There are times when GCC will restrict how we can access the variable returned
from a function/expression.
As the OP says, the code above will compile on MSVC but not GCC.

The problem lies with the assignment operator: (which isn't written quite right)
1
2
3
4
5
6
7
8
Vector Vector::operator = (Vector &p)
{
setX(p.getX());
setY(p.getY());
setZ(p.getZ());

return *this;
}


and the line vect3 = vect2 - vect1;.

The result of the subtraction operator is a (un-named) value Vector variable - and
GCC will not let us take us take a plain old reference to this variable (but MSVC will).

We can re-write the assignment like this:
(Notice that we are now passing by value rather than by reference)
1
2
3
4
5
6
7
8
Vector Vector::operator = (Vector p)
{
setX(p.getX());
setY(p.getY());
setZ(p.getZ());

return *this;
}

This will compile but is not the correct way to write the assignment operator.


The assignment opertator should be setup like this (as already said by wooyen
1
2
3
4
5
6
7
8
Vector& Vector::operator = (const Vector &p)
{
setX(p.getX());
setY(p.getY());
setZ(p.getZ());

return *this;
}

GCC will let us have the constant reference.

However, this will raise some other problems - now that our parameter
is constant, GCC will raise errors with the getX(), getY(), getZ() functions.
These functions have not been declared and defined as constant functions,
so GCC will not allow them to operate on a constant object.
We know that they do not change the object (and do not need to change) the object, so they can be changed to be constant functions.
(Anyway, when you are inside a class function - there is no point in using the Get functions - because you have access to all the class/object variables directly. Using the Get functions is just adding overhead).
1
2
3
4
5
6
7
Vector& Vector::operator = (const Vector &p)
{
  x=p.x;
  y=p.y;
  z=p.z;
return *this;
}






**BTW - as wooyen said - your inheritance structure is not good**
Last edited on
Thanks guestgulken, wooyen, you have been most helpful.

My inheritance structure isn't great I admit, I think I was trying out too many solutions at once so ended up with both an overloaded operator = in Point class and Vector class, is this what you mean or is there something else?

My code now compiles and everything is a-okay, however I get the following message too:

Disallowed system call: SYS_fork

As far as I understand this is not serious but could someone explain what this message is?

Thanks very much again :)

Topic archived. No new replies allowed.