function in class does not compile

Hello!
Maybe someone could help me figure out why these 2 functions do not work when in a class. Specifically, search_ign_case() would not compile.
However, brought out of class lesson and used independently,they work ok.
Here is the code:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

class lesson
{
public:

bool comp_ign_case(char x,char y);
string :: iterator search_ign_case(string&str,const string&subst);

};


bool comp_ign_case(char x,char y);
string :: iterator search_ign_case(string&str,const string&subst);

int main (int argc, const char * argv[]) {
lesson a1;
string s("Windows and Floor will be repaired");

if (a1.search_ign_case(s,"Floors") != s.end())
cout<<endl<<"found!";

return 0;
}

bool comp_ign_case(char x,char y)
{
return tolower(x) == tolower(y);
}

string :: iterator :: lesson search_ign_case(string&str,const string&substr)
{
return search(str.begin(),str.end(),substr.begin(),substr.end(),comp_ign_case);
}

My developing tool is Xcode 3.2.5 in Mac OS X 10.6.5 on MacBook.
Thank you!































hi,

by making comp_ign_case a static function the program compiles and runs (here at least)

the only other change I made was from
1
2
3
4
5
bool comp_ign_case(char x,char y)

//to :

bool lesson::comp_ign_case(char x,char y)


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
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

class lesson
{
public:

	static	bool comp_ign_case(char x,char y);
	string :: iterator search_ign_case(string&str,const string&subst);

};

int main (int argc, const char * argv[])
{
	lesson a1;
	string s("Windows and Floor will be repaired");

	if (a1.search_ign_case(s,"Floor") != s.end())
		cout<<endl<<"found!";

	return 0;
}

bool lesson::comp_ign_case(char x,char y)
{
	return tolower(x) == tolower(y);
}

string :: iterator lesson::search_ign_case(string&str,const string&substr)
{
	return search(str.begin(),str.end(),substr.begin(),substr.end(),comp_ign_case);
}
hi shredded,
many thanks for your help!
Yes, it works on my machine too.

What is it about static that makes search_ign_case() compile?
Last edited on
Hi again,

with non-static member functions the 'this' pointer is passed as a hidden parameter so
that the code knows which object it is working on.

static members dont have the this pointer as they are not linked to an object

1
2
3
4
5
6
7
8
9

//as non-static

bool lesson::comp_ign_case(char x,char y)

//becomes

bool lesson::comp_ign_case(this, char x, char y)


Which has the wrong number of parameters for the search function

Hope I have explained it clear enough.
Shredded
Yes you have.
Thank you very much indeed.
Last edited on
Topic archived. No new replies allowed.