• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

How do I use "set"?

I get the error:  "error C2065: 'set' : undeclared identifier"

For this line of code: "set <int> sNumberUsed;"

But I included "#include <set>" at the top of my header file.

Am I missing something?
0
shaolinfunk
Asked:
shaolinfunk
  • 4
  • 4
  • 3
  • +2
2 Solutions
 
phoffricCommented:
This compiles for me:
#include <set>
using namespace std;

int main() {
   set <int> sNumberUsed;
}

Open in new window

0
 
crysallusCommented:
You need to add:

using std::set;

Open in new window


Just after all your #include's is normally the place to put it.

Read about namespaces here:

http://www.cprogramming.com/tutorial/namespaces.html
0
 
crysallusCommented:
All of the standard C++ libraries are part of the std namespace, so you have 3 options.

1. To include the line:

using namespace std;

Open in new window


at the top of the file after your #include's, as phoffric has done, which essentially brings everything declared in the std namespace into the scope of the file in which it is written.

2. Add a using statement to the top of the file after the #include's for each specific thing from the std namespace that you wish to use. This is normally my preferred option, as the std namespace includes a lot of stuff, and I would say it only makes sense to include all of it if you really are using a lot of it. This is what I've suggested above with:

using std::set;

Open in new window


which just brings set from namespace std into the current scope.

3. Don't add any using statement, and simply write std::set everywhere throughout your code whenever you refer to it. So in your case you would have to write.

std::set<int> sNumberUsed;

Open in new window


and so on whenever you use set. This obviously requires a lot of extra typing, and can make the code a bit messier to read. I wouldn't recommend it.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
phoffricCommented:
Fair summary, crysallus. I see others here at EE like option 3 (but I agree with you - I don't like it either). Option 2 makes sense, but then in large files, I end up with so many using std:xyz statements, that I just go for option 1. But you are right that everything declared in the std namespace into the scope of the file in which it is written. I just treat all standard library terms as reserved words. But, if then bringing in other third party libraries which use these resever words, then the catch-all statement will result in name clashes. So, you are forced to use option 2, which you will likely complete after a dozen compilations until you get all the using statements in correctly.

If two libraries have the word set, and you use them in the same file, then it looks like option 3 will be needed to choose the appropriate one.
0
 
crysallusCommented:
Fair point about 3rd party libraries. If you really do have name clashes, then option 3 it probably is, but I'd only do it if it's absolutely necessary, not for such a simple example as for this question.
0
 
phoffricCommented:
Agreed.
0
 
sarabandeCommented:
there are a few common names like set, list, find, sort, ... in namespace std which might give conflicts when stl is added to existing projects. i don't think there is really a problem with namespaces of other commercial libraries. those are most probably aware of all names in std namespace and won't use any of these. so there is only case left where a developer intentionally (or by ignorance) uses same names as in stl. i would say in those cases it is good when the conflict comes up cause exchanging a class of the stl by an own class of same name is a serious thing which requires much knowledge and discipline and the correct usage of the using clauses surely is a minor point when doing so.

i mostly use std:: prefix cause i always want to make clear that stl functionality is used, specially for novices or programmers not acquainted with stl.

Sara

0
 
phoffricCommented:
I've seen this from many postings on this site and elsewhere (but fortunately not at work):
  std::cout << ... << std::endl;
I agree with crysallus - it can get messier to read; and ease of reading is essential to quick clear maintenance if you are inheriting such a mess.
0
 
Infinity08Commented:
Heh, reminds me of this :

        http://www.experts-exchange.com/Programming/Languages/CPP/Q_24646218.html

We should probably have more discussions like these ...
0
 
shaolinfunkAuthor Commented:
I went with option 2, since I don't know that I use a lot of stuff from "using namespace std"

so far i'm putting "std::" in front string, vector, and set and that's it.

i intend to use pai at some pointr...is that also from the std namespace?
0
 
shaolinfunkAuthor Commented:
so many typos in my last post...

i mean to write "in front OF" and to ask if "pair" also came from the std namespace?
0
 
shaolinfunkAuthor Commented:
nevermind...i just tried it and yes, i think pair is also in the std namespace.

given that i'm using string, vector, set and pair...i think maybe now "using namespace std" is justified instead of typing std:: in front of everything.

anyways, i appreciate the discussion here.  discussing the pros and cons of each option was really helpful!
0
 
sarabandeCommented:
yes, it is std::pair :)

Sara
0
 
crysallusCommented:
@shaolinfunk: Apologies if I've misunderstood you, but it sounds to me from what you've written that you misunderstood option 2 (or I didn't explain it well enough). You only need to put the following at the top of each source file that you are using these classes in.

using std::set;
using std::string;
using std::vector;
using std::pair;

Open in new window

And so on for whatever else from std you need in that source file. If you add these using statements, then you don't need to put std:: anywhere else in that same file that you include the using statement, you can just write set, string etc. without std:: before it. It's just that it sounds to me like you were still putting std:: everywhere throughout your code, which isn't necessary if you have taken option 2 and added those using statements.

Just thought I'd clarify.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now