Stand-alone function

Hi. I was given a task to overload the == operator using a stand-alone function and it should not be a friend. I implemented it as follows. Both parameters a pointers to a string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  
bool operator == (Sentence& a, Sentence& b)
{
	
		cout<<"in the function"<<endl;
	//int counter == 0;
	for(int i = 0; i < a.getNumWords(); i++)
	{
		cout<<"in the function"<<endl;
		if(a[i] != b[i])
		{
			return false;
		}
	}

	
		return true;
	
}


The problem is that I was asked to compare two strings in my main.I got an Undefined reference to error.I was trying to fix the code, only to realise that one of the questions was whether the code will compile and why or why not.So I dont know whether I did the function correctly or not.May someone please help.


This is my main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

int main()
{
	string n = "bunnies";
	Sentence m("bunny");
	
	if(n == m)
	{
		cout<<"They are equal";
	}
	else
	{
		cout<<"not equal "<<endl;
	}
	return 0;
}


Thanks in advanced
You overloaded operator== to compare Sentence a to Sentence b, but in your main function, you are trying to compare string a with Sentence b. You haven't got such function, I assume.
what I dont understand is that when I remve the function I get an error that says no match for operator ==.
I tried using both of them as constants, It works but I get a segmentation fault.Is this really possible, and If not, Why is it not
Topic archived. No new replies allowed.