passing lists to functions

Hello, I have a problem with lists. Basically I would like to create 3 pointers to bunny objects ( lines 5 to 7 ) and place them in a list. Then I would like to pass the list to the function called AnyMutants() (line 9 ) .This function simply steps through the list and perfoms a IsMutant() function on each object. Then return the Boolean result to answer when it finds an occurance when this true. However , I’m really struggling as i want to pass the list as a reference but it is created as a list of pointers to objects???,. I get an error at line 18, saying the operators don’t match the operands?. see code below.

IsMutant() is obviously a member function of mutantType

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


	std::list <mutantType*> bunnyList;
				
	bunnyList.push_front( new mutantType );
	bunnyList.push_front( new mutantType );
	bunnyList.push_front( new mutantType );

	answer =anyMutants( bunnyList);

}

bool anyMutants( const list < mutantType*> tempList)
{ 

	std::list < mutantType*> ::iterator i;
	
	for ( i = tempList.begin();
		i != tempList.end();
		++ i )

        return  (*templist)->Ismutant();

}
(1) You don't pass your list by reference here :P

It should be -> bool anyMutants(const list< mutantType*> & tempList)

(2) Since you pass your list by a const reference, you need a const_iterator ;)

Do it like this -> std::list<mutantType*>::const_iterator i;

Also, make sure your Ismutant function is const correct.

EDIT: Though, now that I look at it again, if what you want is to check if there are any mutants, you should do it in a slightly different way... If you find at least one mutant, return true, and if you find no mutants, return false. The way you do it here, you return false when you find the first non-mutant bunny; that's not what you want.

EDIT 2: I just noticed this. You say 'IsMutant' and in your code you write 'Ismutant'. These two are not the same.
Last edited on
Thanks for that, i've made a few changes based on what you sugguested.see below. However i'm getting and error at line 20 with "no operator matching these operands".It's highlighting the asterix for the pointer again!!

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

	std::list <mutantType*> bunnyList;
				
	bunnyList.push_front( new mutantType );
	bunnyList.push_front( new mutantType );
	bunnyList.push_front( new mutantType );

	answer =anyMutants( bunnyList);

}

bool anyMutants( const list < mutantType*> &tempList) 
{ 
	std::list < mutantType*> ::const_iterator i;
	
	for ( i = tempList.begin();
		 i != tempList.end();
		 ++ i )
	{
       if( (*tempList)->IsMutant()  == true  )
	   {
		   break;
	       return true;
	   }   
	      
	}  
	return false; 

}
Does your IsMutant function return bool?

EDIT: Well, it should be like this -> if( (*i)->IsMutant() == true ) :P

Last edited on
of course ..!! Brilliant ..!!. it works, thanks again m4ster r0shi.
Topic archived. No new replies allowed.