problem with (+=) operator in operator overloading

extern "C"
{
int printf(const char*,...);
int scanf(const char*,...);
int kbhit(void);
void clrscr(void);
int main(void);
}
struct console
{
static void clear()
{
clrscr();
return;
}
static void pause()
{
printf("\npress any key to contniue...");
while(!kbhit());
return;
}
};
struct integer
{
int x;
void set(int i)
{
this->x=i;
return;
}
int get()
{
return this->x;
}
};
struct input
{
static int integer()
{
int x=0;
scanf("%d",&x);
return x;
}
};
struct output
{
static void string(char* str)
{
printf(str);
return;
}
static void integer(integer i)
{
printf("%d",i.get());
return;
}
};
integer operator+=(integer i1,integer i2)
{

i2.set(i2.get()+=i1.get());[error->lvalue required]
return i2;
}
int main(void)
{
integer i1,i2,i3;
console::clear();
output::string("enter two number");
i1.set(input::integer());
i2.set(input::integer());
i3=operator+=(i1,i2);
output::integer(i3);
output::string("=");
output::integer(i1);
output::string("+=");
output::integer(i2);
console::pause();
return 0;
}
Last edited on
I can't read that mess but this is how I would get it done. (As a method)

1
2
3
4
5
6
7
8
9
10
11
class Integer {
public:
	Integer& operator +=(const Integer& i) {
		val += i.val;
		return *this;
	}
	void SetVal(int v) { val = v; }	
	int GetVal() { return val; }
private:
	int val;
};






Last edited on
What kind of compiler is accepting words like:
integer
(I guess this should be int)

i2.set(i2.get()+=i1.get());
I guess if you break it in more lines it would be fine.
lvalue is a value that has an address. For example 2 = a; is illegal 2 does not has an address.

Why are you using printf,scanf(c) and also want to apply operator overloading (c++)? Is there a particular reason?
what is the problem with printf() and scanf() is both these functions are not part of standard library of c++ if yes then what is the problem with that...........
Topic archived. No new replies allowed.