473,882 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operator overloading and copy constructor. Can't find the error.

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(vo id) {
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::operato r+ (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.

Jul 22 '07 #1
22 3635
"clicwar" <cl*****@gmail. comwrote in message
news:11******** **************@ m3g2000hsh.goog legroups.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(vo id) {
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::operato r+ (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.
Jul 22 '07 #2
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...@rock etmail.comwrote :
"clicwar" <clic...@gmail. comwrote in message

news:11******** **************@ m3g2000hsh.goog legroups.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(vo id) {
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::operato r+ (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.

Jul 22 '07 #3
"clicwar" <cl*****@gmail. comwrote in message
news:11******** ************@k7 9g2000hse.googl egroups.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...@rock etmail.comwrote :
>"clicwar" <clic...@gmail. comwrote in message

news:11******* *************** @m3g2000hsh.goo glegroups.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(vo id) {
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::operato r+ (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();
Jul 22 '07 #4
I V
On Sun, 22 Jul 2007 19:11:46 +0000, clicwar wrote:
A simple program with operator overloading and copy constructor:
[...]
Vector::Vector( Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Here, your "copy constructor" doesn't make an actual copy. Are you sure you
want to do that? It seems to me to be likely to cause confusion. Indeed, I
think this is why you're not getting the result you expect:

Vector a(3,1), b(5,2), c, d;
c = a+b;

Now, this calls a.operator+(b), which invokes the copy constructor on b;
so a.operator+ gets passed (10, 4); it then adds this to (3, 1), giving
(13, 5), and returns this, which itself invokes the copy constructor,
giving (26, 10). However, I think this last use of the copy constructor,
in the return, is optional; so the results of calling operator+ depend on
whether or not the compiler decides to optimize out the copy constructor.
This doesn't seem like a very good state of affairs.
Jul 22 '07 #5
Jim, did you mean something like this?

#include <iostream>
#include <string>
using namespace std;

class Vector {
private:
float x,y;
public:
Vector(float u, float v);
Vector();
Vector operator+ (const Vector &a) const;
Vector(const Vector &source);
void Show();
};
void Vector::Show() {
cout <<"(" << x <<"," <<y <<")";
}
Vector::Vector( float u, float v) {
x=u; y=v;
}
Vector::Vector( ) {
x=0; y=0;
}
Vector::Vector( const Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Vector Vector::operato r+ (const Vector &a) const {
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return (temp);
}

int main() {
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;
}

Did you use g++ ? Because now i'm getting (8,3) for both c and e.

Thanks for the patience and for the (void) tip.
Here, your "copy constructor" doesn't make an actual copy. Are you sure you
want to do that?
IV, how can i improve this? I came from a little C background and i
just learned how to do operator overloading.
On 22 jul, 18:59, I V <ivle...@gmail. comwrote:
On Sun, 22 Jul 2007 19:11:46 +0000, clicwar wrote:
A simple program with operator overloading and copy constructor:
[...]
Vector::Vector( Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}

Here, your "copy constructor" doesn't make an actual copy. Are you sure you
want to do that? It seems to me to be likely to cause confusion. Indeed, I
think this is why you're not getting the result you expect:

Vector a(3,1), b(5,2), c, d;
c = a+b;

Now, this calls a.operator+(b), which invokes the copy constructor on b;
so a.operator+ gets passed (10, 4); it then adds this to (3, 1), giving
(13, 5), and returns this, which itself invokes the copy constructor,
giving (26, 10). However, I think this last use of the copy constructor,
in the return, is optional; so the results of calling operator+ depend on
whether or not the compiler decides to optimize out the copy constructor.
This doesn't seem like a very good state of affairs.

Jul 22 '07 #6

clicwar <cl*****@gmail. comwrote in message...
Jim, did you mean something like this?

#include <iostream>
#include <string>
using namespace std;

class Vector { private:
float x,y;
public:
Vector(float u, float v);
Vector();
Vector operator+ (const Vector &a) const;
Vector(const Vector &source);
void Show();
};
void Vector::Show() {
cout <<"(" << x <<"," <<y <<")";
}
Vector::Vector( float u, float v) {
x=u; y=v;
}
Doesn't look like you know about 'initialization lists'. Try this:

Vector::Vector( float u, float v ) : x(u), y(v) {} // note the colon
Vector::Vector( ){ x=0; y=0; }
Vector::Vector( ) : x(0), y(0){}
Vector::Vector( const Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Vector::Vector( Vector const &source) :
x( (source.x)*2 ), y( (source.y)*2 ){}
Vector Vector::operato r+ (const Vector &a) const {
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return (temp);
}

int main() {
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;
}

Did you use g++ ? Because now i'm getting (8,3) for both c and e.
GCC(MinGW)3.3.1 g++, I got:

// out: Data members of the vector c: (8.000000,3.000 000)
// out: Data members of the vector e: (8.000000,3.000 000)
>
Thanks for the patience and for the (void) tip.
Please don't top-post.
--
Bob R
POVrookie
Jul 23 '07 #7
I V
On Sun, 22 Jul 2007 22:54:07 +0000, clicwar wrote:
>Here, your "copy constructor" doesn't make an actual copy. Are you sure you
want to do that?
IV, how can i improve this? I came from a little C background and i
just learned how to do operator overloading.
The obvious copy constructor would be:

Vector::Vector( const Vector &source) {
x = source.x ; y = source.y;
}

(or use initialization lists, as BobR suggests, or just let the compiler
generate the copy-constructor).

This would clearly give you different results, namely (8, 3) rather than
(16, 6). I'm not sure exactly what you're trying to accomplish; why are you
multiplying the x and y elements of your vector by 2 when you make a copy?
Jul 23 '07 #8
"clicwar" <cl*****@gmail. comwrote in message
news:11******** *************@q 75g2000hsh.goog legroups.com...
Jim, did you mean something like this?

#include <iostream>
#include <string>
using namespace std;

class Vector {
private:
float x,y;
public:
Vector(float u, float v);
Vector();
Vector operator+ (const Vector &a) const;
Vector(const Vector &source);
void Show();
};
void Vector::Show() {
cout <<"(" << x <<"," <<y <<")";
}
Vector::Vector( float u, float v) {
x=u; y=v;
}
Vector::Vector( ) {
x=0; y=0;
}
Vector::Vector( const Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Vector Vector::operato r+ (const Vector &a) const {
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return (temp);
}

int main() {
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;
}

Did you use g++ ? Because now i'm getting (8,3) for both c and e.

Thanks for the patience and for the (void) tip.
>Here, your "copy constructor" doesn't make an actual copy. Are you sure
you
want to do that?
IV, how can i improve this? I came from a little C background and i
just learned how to do operator overloading.
On 22 jul, 18:59, I V <ivle...@gmail. comwrote:
>On Sun, 22 Jul 2007 19:11:46 +0000, clicwar wrote:
A simple program with operator overloading and copy constructor:
[...]
Vector::Vector( Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}

Here, your "copy constructor" doesn't make an actual copy. Are you sure
you
want to do that? It seems to me to be likely to cause confusion. Indeed,
I
think this is why you're not getting the result you expect:

Vector a(3,1), b(5,2), c, d;
c = a+b;

Now, this calls a.operator+(b), which invokes the copy constructor on b;
so a.operator+ gets passed (10, 4); it then adds this to (3, 1), giving
(13, 5), and returns this, which itself invokes the copy constructor,
giving (26, 10). However, I think this last use of the copy constructor,
in the return, is optional; so the results of calling operator+ depend on
whether or not the compiler decides to optimize out the copy constructor.
This doesn't seem like a very good state of affairs.
Here's your program cleaned up a bit and extened a little.

#include <iostream>
#include <string>

class Vector
{
private:
float x,y;
public:
Vector(float u, float v);
Vector operator+ (const Vector &a) const;
Vector operator* (const float a ) const;
Vector operator* (const Vector &b) const;
Vector(const Vector &source);
void Show();
};

void Vector::Show()
{
std::cout <<"(" << x <<"," <<y <<")";
}

Vector::Vector( float u = 0, float v = 0): x(u), y(v)
{
}
Vector::Vector( const Vector &source)
{
x = source.x;
y = source.y;
}

Vector Vector::operato r+ (const Vector &a) const
{
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return temp;
}

Vector Vector::operato r* (float a) const
{
Vector temp;
temp.x = x * a;
temp.y = y * a;
return temp;
}

Vector Vector::operato r* (const Vector& a ) const
{
Vector temp;
temp.x = x * a.x;
temp.y = y * a.y;
return temp;
}

int main()
{
Vector a(3,1), b(5,2), c, d;

c = a+b;
d = (a.operator+ (b));
std::cout << "Data members of the vector c: ";
c.Show();
Vector e((a+b));
std::cout << std::endl << "Data members of the vector e: ";
e.Show();
std::cout << "\na * 2 = ";
(a * 2).Show();
std::cout << "\na * b = ";
(a * b).Show();

return 0;
}

All a copy constructor should do is copy. x = x, y = y. Nothing more. If
you want to mulitply, have an operator*

The multiplication of the two Vectors (Vector * Vector) is arbitary. A lot
of times multiplying two vectors would return the dot product. It dpends on
what you want it to do.
Jul 23 '07 #9
>All a copy constructor should do is copy. x = x, y = y. Nothing more. If
>you want to mulitply, have an operator*
Now i understand. I was looking to the copy operator as a way to do
whatever i want with the data members in the moment of the
instantiation of the object.
Now i know that the copy constructor just ... copy.
If i want an additional functionality , i must supply an additional
function outside the copy constructor.

At this moment the fact of implement my own copy constructors seems
very useless, since the compiler provides me a default implementation.
But i'm sure that in the future i will change my mind.

>Please don't top-post
Sorry, i don't know what is top-post. But, surely i will not do again
if you tell me what it is.

Thanks Jim,IV and BobR.

On 22 jul, 22:15, "Jim Langston" <tazmas...@rock etmail.comwrote :
"clicwar" <clic...@gmail. comwrote in message

news:11******** *************@q 75g2000hsh.goog legroups.com...
Jim, did you mean something like this?
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u, float v);
Vector();
Vector operator+ (const Vector &a) const;
Vector(const Vector &source);
void Show();
};
void Vector::Show() {
cout <<"(" << x <<"," <<y <<")";
}
Vector::Vector( float u, float v) {
x=u; y=v;
}
Vector::Vector( ) {
x=0; y=0;
}
Vector::Vector( const Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Vector Vector::operato r+ (const Vector &a) const {
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return (temp);
}
int main() {
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;
}
Did you use g++ ? Because now i'm getting (8,3) for both c and e.
Thanks for the patience and for the (void) tip.
Here, your "copy constructor" doesn't make an actual copy. Are you sure
you
want to do that?
IV, how can i improve this? I came from a little C background and i
just learned how to do operator overloading.
On 22 jul, 18:59, I V <ivle...@gmail. comwrote:
On Sun, 22 Jul 2007 19:11:46 +0000, clicwar wrote:
A simple program with operator overloading and copy constructor:
[...]
Vector::Vector( Vector &source) {
x = (source.x)*2 ; y = (source.y)*2 ;
}
Here, your "copy constructor" doesn't make an actual copy. Are you sure
you
want to do that? It seems to me to be likely to cause confusion. Indeed,
I
think this is why you're not getting the result you expect:
Vector a(3,1), b(5,2), c, d;
c = a+b;
Now, this calls a.operator+(b), which invokes the copy constructor on b;
so a.operator+ gets passed (10, 4); it then adds this to (3, 1), giving
(13, 5), and returns this, which itself invokes the copy constructor,
giving (26, 10). However, I think this last use of the copy constructor,
in the return, is optional; so the results of calling operator+ depend on
whether or not the compiler decides to optimize out the copy constructor.
This doesn't seem like a very good state of affairs.

Here's your program cleaned up a bit and extened a little.

#include <iostream>
#include <string>

class Vector
{
private:
float x,y;
public:
Vector(float u, float v);
Vector operator+ (const Vector &a) const;
Vector operator* (const float a ) const;
Vector operator* (const Vector &b) const;
Vector(const Vector &source);
void Show();

};

void Vector::Show()
{
std::cout <<"(" << x <<"," <<y <<")";

}

Vector::Vector( float u = 0, float v = 0): x(u), y(v)
{

}

Vector::Vector( const Vector &source)
{
x = source.x;
y = source.y;

}

Vector Vector::operato r+ (const Vector &a) const
{
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return temp;

}

Vector Vector::operato r* (float a) const
{
Vector temp;
temp.x = x * a;
temp.y = y * a;
return temp;

}

Vector Vector::operato r* (const Vector& a ) const
{
Vector temp;
temp.x = x * a.x;
temp.y = y * a.y;
return temp;

}

int main()
{
Vector a(3,1), b(5,2), c, d;

c = a+b;
d = (a.operator+ (b));
std::cout << "Data members of the vector c: ";
c.Show();
Vector e((a+b));
std::cout << std::endl << "Data members of the vector e: ";
e.Show();
std::cout << "\na * 2 = ";
(a * 2).Show();
std::cout << "\na * b = ";
(a * b).Show();

return 0;

}

All a copy constructor should do is copy. x = x, y = y. Nothing more. If
you want to mulitply, have an operator*

The multiplication of the two Vectors (Vector * Vector) is arbitary. A lot
of times multiplying two vectors would return the dot product. It dpends on
what you want it to do.

Jul 23 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
3102
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
7
4680
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++ copyconstructor1.cpp #copyconstructor1.cpp: In function `int main()': #copyconstructor1.cpp:86: no match for `sample& = sample' operator #copyconstructor1.cpp:53: candidates are: sample sample::operator=(sample&) ...
7
1837
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class xydata with operator- overloaded: class xydata { public:
16
2520
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row and _col which are respectively the array to store the data in, the number of rows and the number of columns. _m is of type T, and is size _row * _col * sizeof (T). I've managed to overload the subscript operator like this:
17
2362
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks << endl;
0
2063
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the increment operator in a class and then defining a derived class and using the derived class. When I try to use the increment operator on an instance of the derived class (as an instance of the derived class, not when used as an instance of the base...
3
3291
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
2
2086
by: KiranJyothi | last post by:
Can anyone please tell me how can I rectify my compile time error in this program. /********************************HEADER FILE***********************************/ #ifndef POLYNOMIAL_H // conditional compilation #define POLYNOMIAL_H # include <iostream> using namespace std;
7
1748
by: Rahul | last post by:
Hi Everyone, I was overloading the operator= function as a class member function, #include <iostream.h> class A { int value; public : A& operator = (const A& ref)
0
9777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10829
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10403
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9556
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7113
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4599
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.