views:

65

answers:

2

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.

A: 

(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>
{
    // ...
};
Cogwheel - Matthew Orlando
Your initial answer solved most of my problems, however, now when I compile I get - E2034 Cannot convert 'const SOURCE_TYPE' to 'SOURCE_DATA *'
photo_tom
Might be simpler to make a new question with the revised code and error message. It can get a bit confusing when a single thread is used to solve several problems over a period of time :)
Cogwheel - Matthew Orlando
At first glance, though, it sounds like you're instantiating the binary_function with the wrong arguments. Perhaps you're passing the pointer type in as a parameter when you should just be passing the base type? (the is_Selected_Source template handles converting it to a pointer)
Cogwheel - Matthew Orlando
A: 

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.

Jerry Coffin