Error while doing set_difference: Variable result

2019-03-06 22:23发布

问题:

i had declared a set variable outside a function globally.

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

Now i try to populate that structure inside a function.

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.

I get this compilation error:

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

edit:

the removal of underscore didnot work. i tried now with :

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

This gives me another compilation error.:

"/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.

回答1:

This code compiles for me.

Perhaps your compiler is complaining about your underscore usage? Since it says:

Variable result is not a structure.

Whereas you declared _result_.

See What are the rules about using an underscore in a C++ identifier? (as recommended by Mat).



回答2:

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

Add type parameter in inserter definition:

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