std::binary_function 用法的语法

发布于 2024-09-07 21:55:11 字数 1048 浏览 2 评论 0原文

我是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好久不见√ 2024-09-14 21:55:11

(只有当您没有意外地从问题中省略某些代码时,这才有意义,并且可能无法解决您遇到的确切问题)

您正在使用 is_Selected_Source 作为模板,即使您没有将其定义为之一。第二个代码片段中的最后一行应为 std::bind1st(is_Selected_Source()...

或者您确实想将其用作模板,在这种情况下您需要添加模板声明到结构体。

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

(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>
{
    // ...
};
上课铃就是安魂曲 2024-09-14 21:55:11

据猜测(尽管这只是猜测),问题在于 std::remove_copy 需要一个,但您提供了一个谓词。要使用谓词,您需要使用 std::remove_copy_if (然后您需要注意@Cogwheel的答案)。

我还要指出的是:

LIST; *localList = new LIST;;

看起来不对——我猜你的意图是:

LIST *locallist = new LIST;

相反。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文