Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have error improve use of typedef obj in this c++ program.
This is swapping a program.
Please help me?

And explain the error and how to clear this error.
C++
#include
#include

class swap
{
int a,b;
public:
void getdata(void);
void change(void);
void display(void);
};

void swap :: getdata(void)
{
cout<<" enter the a value: ";
cin>>a;
cout<<"enter the b value: ";
cin>>b;
}

void swap :: change(void)
{
a = a+b;
b = a-b;
a = a-b;
}

void swap :: display(void)
{
cout<<"a value: "<<"\n";
cout<<"b value: "<<"\n";
}

void main()
{
clrscr();
class obj;
obj.getdata();
obj.display();
getch();
}
Posted
Updated 27-Sep-11 9:14am
v2

You use of the class keyword is wrong when defining a swap class variable.
Instead of using
C++
void main()
{
	clrscr();
	class obj;
	obj.getdata();
	obj.display();
	getch();
}

use
C++
void main()
{
	clrscr();
	swap obj;
	obj.getdata();
	obj.display();
	getch();
}


You can read these short tutorials to increase your knowledge about classes and variables:
Classes (I)[^]
Variables. Data Types.[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Sep-11 16:32pm    
A good catch, my 5. I credited your solution in mine. My point was different -- the swap algorithm itself. Please see my update under [EDIT].
--SA
André Kraak 27-Sep-11 16:39pm    
Thanks.
kschandru 27-Sep-11 22:54pm    
@André Kraak: Thank u... thank for find out my error..... :-)
Probably you got an assignment two swap to integers without using temporary (stack) variable, otherwise you would honestly use a temporarily variable with no need to goof around.

This is a classical exercise which is very simple, so everyone is supposed to find the solution without reading anything, but of course based on knowledge of simple bit arithmetic.

[EDIT]
You result is correct though, but not elegant. There is a simple and universal way to swap any two bit patterns not using arithmetic of numeric types and not using any temporary storage for a value.
Your problem is the syntax. Use the advice by André Kraak.
[END EDIT]

Sorry, I don't want to tell you the solution, otherwise it would be not useful to you but would be cheating against people who honestly solve their assignment by themselves.

—SA
 
Share this answer
 
v7
Comments
André Kraak 27-Sep-11 16:42pm    
Good point about the swap, my 5. I will keep my lips sealed.
Sergey Alexandrovich Kryukov 27-Sep-11 17:05pm    
Thank you, André.
From your response, it looks like you know how to do it right...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900