473,382 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

remove_if and functions overloading

Hi,

if I compile the following code I get the error:

RemoveIf.cpp(19) : error C2914: 'remove_if' : cannot deduce template
argument as function argument is ambiguous

#include <algorithm>
#include <vector>

template<typename T>
class vector_t
: public std::vector<T>
{
bool TestRemove(long Pos) const //(1)
{
return false;
}
static bool TestRemove(const T &) //(2)
{
return false;
}
public:
void RemoveIf()
{
erase(std::remove_if(begin(), end(), &vector_t<T>::TestRemove),
end()); //this is line 19
}
};

typedef struct point
{
long x;
long y;
long f;
} point;

int main()
{
vector_t<point> vp;
vp.RemoveIf();

return 0;
}
How may I force the remove_if to use the TestRemove (2) function as
predicate?

TIA.
Marco.
Jul 22 '05 #1
2 2424
On 7 Apr 2004 02:52:48 -0700, ma***********@virgilio.it
(marco_segurini) wrote:
Hi,

if I compile the following code I get the error:

RemoveIf.cpp(19) : error C2914: 'remove_if' : cannot deduce template
argument as function argument is ambiguous

#include <algorithm>
#include <vector>

template<typename T>
class vector_t
: public std::vector<T>
{
bool TestRemove(long Pos) const //(1)
{
return false;
}
static bool TestRemove(const T &) //(2)
{
return false;
}
public:
void RemoveIf()
{
bool (*remover)(const T&) = &vector_t<T>::TestRemove;
erase(std::remove_if(begin(), end(), remover),
end()); //this is line 19
How may I force the remove_if to use the TestRemove (2) function as
predicate?


Either the above, or add a static cast to select the function type:

erase(
std::remove_if(
begin(),
end(),
static_cast<bool (*)(const T&)>(&vector_t<T>::TestRemove)
),
end()
);

When taking the address of an overloaded function, you need a target
type for the compiler to be able to work out which overload you are
referring to. You can specify the target type using an assignment or a
static_cast.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
On 7 Apr 2004 02:52:48 -0700, ma***********@virgilio.it
(marco_segurini) wrote:
Hi,

if I compile the following code I get the error:

RemoveIf.cpp(19) : error C2914: 'remove_if' : cannot deduce template
argument as function argument is ambiguous

#include <algorithm>
#include <vector>

template<typename T>
class vector_t
: public std::vector<T>
{
bool TestRemove(long Pos) const //(1)
{
return false;
}
static bool TestRemove(const T &) //(2)
{
return false;
}
public:
void RemoveIf()
{
bool (*remover)(const T&) = &vector_t<T>::TestRemove;
erase(std::remove_if(begin(), end(), remover),
end()); //this is line 19
How may I force the remove_if to use the TestRemove (2) function as
predicate?


Either the above, or add a static cast to select the function type:

erase(
std::remove_if(
begin(),
end(),
static_cast<bool (*)(const T&)>(&vector_t<T>::TestRemove)
),
end()
);

When taking the address of an overloaded function, you need a target
type for the compiler to be able to work out which overload you are
referring to. You can specify the target type using an assignment or a
static_cast.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: CupOfWater | last post by:
Hi, I'm getting some errors using remove_if that I can not fix at all. Let me paste my code and also the error here. I went on IRC and people were mostly no help. The compiler is g++ 3.2.3. ...
5
by: Wang Tong | last post by:
I have the following code, which removes a pair (string, int) from the set. I defined a predicate that returns true if the string value matches. But I am getting compiler error on the remove_if...
1
by: Paavo K | last post by:
Hi, can anyone tell what is wrong with following code : //..Code class removeTest { public: bool operator()(map<int, string>::value_type &d) const { // Just returning false to simplify...
2
by: marco_segurini | last post by:
Hi, if I compile the following code I get the error: RemoveIf.cpp(19) : error C2914: 'remove_if' : cannot deduce template argument as function argument is ambiguous #include <algorithm>...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
2
by: Adam Hartshorne | last post by:
Hi All, I want to do the following, I have a std::vector<Patch> called say vs. Now there is a bool variable in the class called outlier, which may be access by getOutlierStatus() method. ...
3
by: Jon Rea | last post by:
Hello all, Sorry if this is long, but I wanted to be specific... Can anyone shed any light on the following errors in the context of the following example code: cullMethod1(); // compiles,...
3
by: Angus | last post by:
Hi I have a class CRequest with a function: bool IsFinished() const; I have a list of these CRequests - list<CRequestmylist. I create a function object to find if a CRequest is finished:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.