C++ question

W

web1110

Hi y'all,

I was playing with C++ and tried this program:

#include "stdafx.h"
#include <iostream.h>

class prop
{
private:
int x;
int y;

public:
prop(int x, int y);
int& X();
int& operator= (int);
};

prop::prop(int x, int y)
{
prop::x=x;
prop::y=y;
}

int& prop::X()
{
return x;
}

int& prop::eek:perator=(int=0)
{
return x;
}

int main(int argc, char* argv[])
{
prop a(1, 2);
cout << "a.X=" << a.X() << '\n';
a.X=10; <-----------------------------------
cout << "a.X=" << a.X() << '\n';
return 0;
}

The marked line produces the error:

S:\tmp\VS60\Props\Props.cpp(39) : error C2659: '=' : overloaded function
as left operand

Can someone explain why?

Thanx,
Bill
 
T

Tim Love

web1110 said:
Hi y'all,
I was playing with C++ and tried this program:
#include "stdafx.h"
#include <iostream.h>
class prop
{
private:
int x;
int y;
public:
prop(int x, int y);
int& X();
int& operator= (int);
};
prop::prop(int x, int y)
{
prop::x=x;
prop::y=y;
}
int& prop::X()
{
return x;
}
int& prop::eek:perator=(int=0)
{
return x;
}
int main(int argc, char* argv[])
{
prop a(1, 2);
cout << "a.X=" << a.X() << '\n';
a.X=10; <-----------------------------------
cout << "a.X=" << a.X() << '\n';
return 0;
}
The marked line produces the error:
S:\tmp\VS60\Props\Props.cpp(39) : error C2659: '=' : overloaded function
as left operand
Can someone explain why?
My compiler gives this helpful error message for the
int& prop::eek:perator=(int=0)
line -
"An overloaded operator cannot have default arguments."
 
T

Thomas Maier-Komor

web1110 said:
Hi y'all,

I was playing with C++ and tried this program:

#include "stdafx.h"
#include <iostream.h>

class prop
{
private:
int x;
int y;

public:
prop(int x, int y);
int& X();
int& operator= (int);
};

prop::prop(int x, int y)
{
prop::x=x;
prop::y=y;
}

int& prop::X()
{
return x;
}

int& prop::eek:perator=(int=0)
{
return x;
}

int main(int argc, char* argv[])
{
prop a(1, 2);
cout << "a.X=" << a.X() << '\n';
a.X=10; <-----------------------------------

try:
a.X()=10;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top