C
clicwar
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:perator+ (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.
#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:perator+ (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.