-1

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.
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier - try with a variable name without a leading underscore – Mat Dec 26 '11 at 08:57
  • tried that but the problem still appears. – Vijay Dec 26 '11 at 09:15
  • The `std::copy` error suggests something different is going on here. Are you trying to copy from a `std::multimap` to a `std::set` (like in [this answer](http://stackoverflow.com/a/8560322/78845))? You really need to supply us with the *actual* code you are trying to build! – johnsyweb Dec 26 '11 at 09:38
  • 1
    Your edit describes a new issue, which should be posted as a new question, were it not [already asked](http://stackoverflow.com/questions/8635186/). SO uses a Q&A, rather than forum, format. If you have distinct questions, they should be posted as such. – outis Dec 26 '11 at 21:56

2 Answers2

2

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).

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • as i had updated .i removed the underscore and tried another method of inserting.even that did not work.i also changed the name to "myresult" even that did not work. – Vijay Dec 26 '11 at 09:08
  • Can you provide a short but complete example? I can't reproduce the error with the code you've given, even with SUNPro. – johnsyweb Dec 26 '11 at 09:19
  • the problems seems that we have our own set_difference defined in our source code. in algorithm.cc as you can see teh compilation error.i am just trying to figure a way out to use only standard c++ set_difference.but i am not knowing how to do it. – Vijay Dec 26 '11 at 09:23
  • `SUNWspro/prod/include/CC/Cstd/algorithm.cc` is SunPRO's implementation of the algorithms from the Standard Library. The error is in one of your 6572+ lines of code. This is why I asked you to provide an [SSCCE](http://sscce.org/). I can't help you otherwise. – johnsyweb Dec 26 '11 at 09:29
  • I am afraid i can't give you an example.coz when i tried the same line in a sample c++ program it compiled perfectly:( – Vijay Dec 26 '11 at 09:36
  • Now you see *my* problem! I'm afraid without an example, I cannot help any further. – johnsyweb Dec 26 '11 at 09:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6134/discussion-between-peter-and-johnsyweb) – Vijay Dec 26 '11 at 09:53
0

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()));
9dan
  • 4,222
  • 2
  • 29
  • 44