std::binary_function 用法的语法
我是使用 STL 算法的新手,目前遇到语法错误。我的总体目标是像在 C# 中使用 Linq 一样过滤源列表。在 C++ 中可能还有其他方法可以做到这一点,但我需要了解如何使用算法。
我用作函数适配器的用户定义函数对象是
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;
}
};
在我的主程序中,我使用如下 -
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));
我在 Rad Studio 2010 中收到以下错误。该错误意味着“您的源文件使用了 typedef 符号变量应出现在表达式中的位置。
“E2108 typedef 'is_Selected_Source'使用不当”
编辑 - 在VS2010(具有更好的编译器诊断)中进行了更多实验后,我发现问题在于remove_copy的定义仅允许一元函数。我将函数更改为一元函数并使其正常工作。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(只有当您没有意外地从问题中省略某些代码时,这才有意义,并且可能无法解决您遇到的确切问题)
您正在使用
is_Selected_Source
作为模板,即使您没有将其定义为之一。第二个代码片段中的最后一行应为std::bind1st(is_Selected_Source()
...或者您确实想将其用作模板,在这种情况下您需要添加模板声明到结构体。
(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 readstd::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.
据猜测(尽管这只是猜测),问题在于
std::remove_copy
需要一个值,但您提供了一个谓词。要使用谓词,您需要使用std::remove_copy_if
(然后您需要注意@Cogwheel的答案)。我还要指出的是:
看起来不对——我猜你的意图是:
相反。
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 usestd::remove_copy_if
(and then you'll want to heed @Cogwheel's answer).I'd also note that:
Looks wrong -- I'd guess you intended:
instead.