checking equal object


how can i check that two objects are equal or not.i have a problem in my code.what should i do? i think both my get method and equal method are wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  #include<iostream>
using namespace std;
class state{
	public:
		int num;
		 state* start;
		 state* q1;
		 state* q2;
		 state* q3;
		 state* q4;
		 state* q5;
		 state* now;
		void getTheInput(char);
		bool equal(state*,state*);
		void get(state*,state*);
};
void state::get(state *a,state*b){
	a=b;
}
bool state::equal (state *a,state* b){
	return a==b; 
}
void state::getTheInput(char a)
{
	if(a=='a' || a=='b')
	{
		if(equal(now,start) && a=='a'){
			cout<<"1";
			get(now,q1);
		}
		if(equal(now,start) && a=='b')
			get(now,start);
		if(equal(now,q1) && a=='a')
			cout<<"1";
			get(now,q1);
		if(equal(now,q1) && a=='b')
			get(now,q2);
		if(equal(now,q2) && a=='a')
			get(now,q3);
		if(equal(now,q2) && a=='b')
			get(now,start);
		if(equal(now,q3) && a=='a')
			get(now,q1);
		if(equal(now,q3) && a=='b')
			get(now,q4);
		if(equal(now,q4) && a=='a')
			get(now,q3);
		if(equal(now,q4)&& a=='b'){
			get(now,q5);
			cout<<"Got it!!!";
		}
		if(equal(now,q5) && a=='a')
			get(now,q1);
		if(equal(now,q5) && a=='b')
			get(now,start);
	}
	else {
		cout<<"Wrong input!! just 'a' or 'b'";
	}
}

int main()
{
	char a;
	state *test=new state();
	test->now=test->start;

	for(;;)
	{
		cin>>a;
		test->getTheInput(a);
	}
}
Last edited on
I'm not quite sure what exactly you're trying to do here. If something seems wrong to you, it probably is. If you're not getting the desired results, something is wrong. Between your cryptic variable names, and nothing to really go on except for some short code, I don't know how we can help you.

What is the desired result? What is the current result? What is the purpose of all of the different state pointers??
Your == operator is only comparing pointers, it's checking that the 2 states are the same instance rather than the same state.

If thats what you intended then all well and good, but its more usual to check the state is the same, so you will need to store the current state as a variable so you can compare that instead.
Topic archived. No new replies allowed.