Re: Operator overloading and copy constructor. Can't find the error.
"clicwar" <clicwar@gmail.com> wrote in message
news:1185134796.453311.9430@k79g2000hse.googlegroups.com...
First of all, thank you Jim. It worked ( but i don't know why the
simple addition of the const modifier can do that. Could you tell
me? ).
The output now is:
Data members of the vector c: (13,5)
Data members of the vector e: (13,5)
But it should be (16,6) for both c and e.
Any suggestions?
Thanks.
On 22 jul, 16:20, "Jim Langston" <tazmas...@rocketmail.com> wrote:
"clicwar" <clic...@gmail.com> wrote in message
news:1185131506.971858.250680@m3g2000hsh.googlegroups.com...
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u, float v);
Vector(void);
Vector operator+ (Vector a);
Vector(Vector &source);
void Show(void);
};
void Vector::Show(void) {
cout <<"(" << x <<"," <<y <<")";
}
Vector::Vector(float u, float v) {
x=u; y=v;
}
Vector::Vector(void) {
x=0; y=0;
}
Vector::Vector(Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Vector Vector::operator+ (Vector a) {
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return temp;
}
int main(void) {
Vector a(3,1), b(5,2), c, d;
c = a+b;
d = a.operator+ (b);
cout << "Data members of the vector c: ";
c.Show();
Vector e(a+b);
cout <<endl << "Data members of the vector e: ";
e.Show();
return 0;
}
The compiler (g++ -pedantic -W -Wall) says:
teste.cpp: In function `int main()':
teste.cpp:36: error: no matching function for call to
`Vector::Vector(Vector)'
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:37: error: no matching function for call to
`Vector::Vector(Vector)'
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:40: error: no matching function for call to
`Vector::Vector(Vector)'
teste.cpp:24: note: candidates are: Vector::Vector(Vector&)
teste.cpp:21: note: Vector::Vector()
teste.cpp:18: note: Vector::Vector(float, float)
Without the copy constructor Vector::Vector(Vector &source) , it works
fine.
Would anyone know what is wrong in the code?
Thanks in advance.
Change it to:
Vector::Vector( const Vector& a )
and it should work.
Well, I get even a weirder result with your code (26.3 or something). Not
sure why, but if you fix your operator+ it'll show the correct answer of
16.6
Vector operator+ (const Vector& a) const;
Operators and constructors have specific signatures. const is part of them
and I believe references are also. I really don't know what was going on
that was giving us the wrong results (I.E. no clue what the compiler was
donig) and don't really care, cause fixing the operator+ fixes the problem.
As to why it didn't work without your constructor having const, that's
because it didn't recognize it as a copy constructor.
Which makes me think, changing it from a copy to a reference probably means
your copy constructor is not working right, let me look at it...
Your copy constructor is mulitplying the values by 2? That's not what a copy
constructor is supposed to do.
Incidently, you can merge your 2 constructors into one.
Vector(float u, float v);
Vector::Vector(float u = 0, float v = 0): x(u), y(v)
{
}
Also, this is C++. We don't put (void) when there are no parameters to a
method/function. We just use () I.E.
void Show();