执行set_difference时出错:变量结果不是结构

时间:2011-12-26 08:53:24

标签: c++ stl set set-difference

我已在全局函数外声明了一个set变量。

std::set<std::string> s1;
std::set<std::string> s2;   
std::set<std::string> intersect;    
std::set<std::string> _result_; //here is the declaration

现在我尝试在函数中填充该结构。

s1.insert("1-1");
s2.insert("1-1");
std::set_intersection( s1.begin(), s1.end(), s2.begin(), s2.end(),std::insert_iterator< std::set<std::string> >( intersect, intersect.begin() ) );
    std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(),std::inserter(_result_, _result_.end()));//this is where the error is coming.

我收到此编译错误:

"cacup_bsc.cc", line 6572: Error: Variable result is not a structure.

编辑:

删除下划线不起作用。 我现在尝试用:

std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(),std::insert_iterator< std::set<std::string> >(result_, result_.end() ) );

这给了我另一个编译错误。:

"/export/SunStudio/SUNWspro/prod/include/CC/Cstd/./algorithm.cc", line 2161: Error: Overloading ambiguity between "std::copy<__rwstd::__rb_tree<std::string, std::string, __rwstd::__ident<std::string, std::string>, std::less<std::string>, std::allocator<std::string>>::const_iterator, std::insert_iterator<std::set<std::string>>>(__rwstd::__rb_tree<std::string, std::string, __rwstd::__ident<std::string, std::string>, std::less<std::string>, std::allocator<std::string>>::const_iterator, __rwstd::__rb_tree<std::string, std::string, __rwstd::__ident<std::string, std::string>, std::less<std::string>, std::allocator<std::string>>::const_iterator, std::insert_iterator<std::set<std::string>>)" and "copy<__rwstd::__rb_tree<std::string, std::string, __rwstd::__ident<std::string, std::string>, std::less<std::string>, std::allocator<std::string>>::const_iterator, std::insert_iterator<std::set<std::string>>>(__rwstd::__rb_tree<std::string, std::string, __rwstd::__ident<std::string, std::string>, std::less<std::string>, std::allocator<std::string>>::const_iterator, __rwstd::__rb_tree<std::string, std::string, __rwstd::__ident<std::string, std::string>, std::less<std::string>, std::allocator<std::string>>::const_iterator, std::insert_iterator<std::set<std::string>>)".
"cacup_bsc.cc", line 6572:     Where: While instantiating "std::set_difference<__rwstd::__rb_tree<std::string, std::string, __rwstd::__ident<std::string, std::string>, std::less<std::string>, std::allocator<std::string>>::const_iterator, __rwstd::__rb_tree<std::string, std::string, __rwstd::__ident<std::string, std::string>, std::less<std::string>, std::allocator<std::string>>::const_iterator, std::insert_iterator<std::set<std::string>>>(__rwstd::__rb_tree<std::string, std::string, __rwstd::__ident<std::string, std::string>, std::less<std::string>, std::allocator<std::string>>::const_iterator, __rwstd::__rb_tree<std::string, std::string, __rwstd::__ident<std::string, std::string>, std::less<std::string>, std::allocator<std::string>>::const_iterator, __rwstd::__rb_tree<std::string, std::string, __rwstd::__ident<std::string, std::string>, std::less<std::string>, std::allocator<std::string>>::const_iterator, __rwstd::__rb_tree<std::string, std::string, __rwstd::__ident<std::string, std::string>, std::less<std::string>, std::allocator<std::string>>::const_iterator, std::insert_iterator<std::set<std::string>>)".
"cacup_bsc.cc", line 6572:     Where: Instantiated from non-template code.

2 个答案:

答案 0 :(得分:2)

This code compiles for me.

也许你的编译器抱怨你的下划线用法?既然它说:

  

变量 result 不是结构。

你宣布_result_

请参阅What are the rules about using an underscore in a C++ identifier?(根据Mat的建议)。

答案 1 :(得分:0)

  

std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(),std::inserter(_result_, _result_.end()));

在插入器定义中添加类型参数:

std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter< std::set<std::string> >(_result_, _result_.end()));