I've got two member functions:

//Particle.h

class Particle
{
public:
	bool isDead()const; 

	static bool isDead(const Particle & par);
};

//Particle.cpp

bool Particle::isDead()const
{
	return(xPos<=0 || xPos>=SCREEN_WIDTH || yPos<=0 || yPos>=GRASS_HEIGHT);
}

bool Particle::isDead(const Particle & par)
{
	return(par.xPos<=0 || par.xPos>=SCREEN_WIDTH || par.yPos<=0 || par.yPos>=GRASS_HEIGHT);
}

But when a try to use the other function as a predicate for a remove_if call, I get an ambiguity error.

//Cursor.cpp

//The statement that causes the error
particleContainer.remove_if(Particle::isDead);

1>c:\users\eric\documents\visual studio 2008\projects\duck hunt\duck hunt\cursor.cpp(174) : error C2914: 'std::list<_Ty>::remove_if' : cannot deduce template argument as function argument is ambiguous
1>        with
1>        [
1>            _Ty=Particle
1>        ]
1>c:\users\eric\documents\visual studio 2008\projects\duck hunt\duck hunt\cursor.cpp(174) : error C2784: 'void std::list<_Ty>::remove_if(_Pr1)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'
1>        with
1>        [
1>            _Ty=Particle
1>        ]
1>        c:\program files\microsoft visual studio 9.0\vc\include\list(944) : see declaration of 'std::list<_Ty>::remove_if'

(particleContainer is a list of Particles(list<Particle> particleContainer))

I can't see the ambiguity in this statement. What am I doing wrong?

Recommended Answers

All 3 Replies

remove_if only knows the name of the method, but the class defines two methods by that name. So remove_if cannot decide which one to call. You can disambiguate with a cast to the more suitable predicate.

particleContainer.remove_if((bool(*)(const Particle&))Particle::isDead);

Tried it in VS2005 and linux g++, both give errors even if you change parameter types. Works if you change function name or use a forwarding function but I guess you knew that.

class Particle{
public:	
	bool isDead()const
		{return true;};  	
		static bool isDead(const Particle & par)
		{return true;};
		static bool isDead2(const Particle & par){return isDead(par);};
};

int main()
{
	list<Particle> lst;

	Particle p1, p2;
	lst.push_back(p1);
	lst.push_back(p2);
	
	lst.remove_if(Particle::isDead2);
};

Why do you have 2 isDead function? From your code, it seems like isDead function needs
to be just a function like so :

struct Particle{
 float posX, posY;
};

bool isParticleDead(const Particle& p){ return isOutOfScreen(p);}

int main(){
 list<Particle> p;
 p.push_back( Particle() );
 p.push_back( Particle() );
 p.remove_if(isParticleDead);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.