开发者

Syntax for std::binary_function usage

I'm a newbie at using the STL Algorithms and am currently stuck on a syntax error. My overall goal of this is to filter the source list like you would using Linq in c#. There may be other ways to do this in C++, but I need to understand how to use algorithms.

My user-defined function object to use as my function adapter is

struct is_Selected_Source : public std::binary_function<SOURCE_DATA *, SOURCE_TYPE, bool>
{
bool operator()(SOURCE_DATA * test,  SOURCE_TYPE ref)const
    {
    if (ref == SOURCE_All)
        return true;
    return test->Value == ref;
    }
};

And in my main program, I'm using as follows -

typedef std::list<SOURCE_DATA *> LIST;
LIST; *localList = new LIST;;
LIST* msg = GLOBAL_DATA->MessageList;
SOURCE_TYPE _filter_Msgs_Source = SOURCE_TYPE::SOURCE_All;

std::remove_copy(msg->begin(), msg->end(), localList->begin(),
    std::bind1st(is_Selected_Source<SOURCE_DATA*, SOURCE_TYPE>(), _filter_Msgs_Source));

What I'm getting the following error in Rad Studio 2010. The error means "Your source file used a typedef symbol where a variable should appear in an expression. "

"E2108 Improper开发者_运维知识库 use of typedef 'is_Selected_Source'"


Edit - After doing more experimentation in VS2010, which has better compiler diagnostics, I found the problem is that the definition of remove_copy only allows uniary functions. I change the function to uniary and got it to work.


(This is only relevant if you didn't accidentally omit some of your code from the question, and may not address the exact problem you're having)

You're using is_Selected_Source as a template even though you didn't define it as one. The last line in the 2nd code snippet should read std::bind1st(is_Selected_Source()...

Or perhaps you did want to use it as a template, in which case you need to add a template declaration to the struct.

template<typename SOURCE_DATA, typename SOURCE_TYPE>
struct is_Selected_Source : public std::binary_function<SOURCE_DATA *, SOURCE_TYPE, bool>
{
    // ...
};


At a guess (though it's only a guess) the problem is that std::remove_copy expects a value, but you're supplying a predicate. To use a predicate, you want to use std::remove_copy_if (and then you'll want to heed @Cogwheel's answer).

I'd also note that:

LIST; *localList = new LIST;;

Looks wrong -- I'd guess you intended:

LIST *locallist = new LIST;

instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜