Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::binary_函数用法的语法_C++_Stl_C++builder - Fatal编程技术网

C++ std::binary_函数用法的语法

C++ std::binary_函数用法的语法,c++,stl,c++builder,C++,Stl,C++builder,我是使用STL算法的新手,目前遇到语法错误。我的总体目标是像在c#中使用Linq一样过滤源代码列表。在C++中可能有其他的方法来做这件事,但是我需要理解如何使用算法。 用作函数适配器的用户定义函数对象是 struct is_Selected_Source : public std::binary_function<SOURCE_DATA *, SOURCE_TYPE, bool> { bool operator()(SOURCE_DATA * test, SOURCE_TYPE r

我是使用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;
    }
};
struct已被选中\u源代码:public std::binary\u函数
{
布尔运算符()(源数据*测试,源类型引用)常量
{
如果(ref==SOURCE\u All)
返回true;
返回测试->值==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));
typedef标准::列表;
名单*localList=新列表;;
LIST*msg=GLOBAL_DATA->MessageList;
SOURCE\u TYPE\u filter\u Msgs\u SOURCE=SOURCE\u TYPE::SOURCE\u All;
删除拷贝(msg->begin(),msg->end(),localList->begin(),
std::bind1st(是否选择了源(),\u过滤器\u Msgs\u源));
我在Rad Studio 2010中遇到以下错误。该错误表示“源文件使用了typedef符号,其中变量应出现在表达式中。”

“E2108未正确使用typedef‘已选择源’”


编辑- 在VS2010中做了更多的实验后,我发现问题在于remove_copy的定义只允许单值函数。我将函数更改为uniary并使其工作。

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

您正在使用
is\u Selected\u Source
作为模板,即使您没有将其定义为模板。第二个代码段中的最后一行应该是
std::bind1st(is\u Selected\u Source()

或者您确实希望将其用作模板,在这种情况下,您需要向结构添加模板声明

template<typename SOURCE_DATA, typename SOURCE_TYPE>
struct is_Selected_Source : public std::binary_function<SOURCE_DATA *, SOURCE_TYPE, bool>
{
    // ...
};
模板
已选择结构\u源:public std::binary\u函数
{
// ...
};
猜测(虽然只是猜测)问题是
std::remove\u copy
需要一个值,但您提供了一个谓词。要使用谓词,您需要使用
std::remove\u copy\u if
(然后您需要注意@Cogwheel的答案)

我还要指出:

LIST; *localList = new LIST;;
看起来不对——我猜你是有意的:

LIST *locallist = new LIST;

相反。

您最初的答案解决了我的大部分问题,但是,现在当我编译我的get-E2034时,无法将“const SOURCE_TYPE”转换为“SOURCE_DATA*”,用修改后的代码和错误消息提出新问题可能会更简单。如果在一段时间内使用单个线程解决多个问题,可能会有点混乱:)不过,乍一看,这听起来像是用错误的参数实例化二进制函数。也许您是在传递指针类型作为参数,而您应该只传递基类型?(已选定源模板处理将其转换为指针的操作)