Solved

How do I use "set"?

Posted on 2011-03-03
14
333 Views
Last Modified: 2013-12-14
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
Comment
Question by:shaolinfunk
  • 4
  • 4
  • 3
  • +2
14 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
ID: 35032751
This compiles for me:
#include <set>
using namespace std;

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

Open in new window

0
 
LVL 8

Assisted Solution

by:crysallus
crysallus earned 250 total points
ID: 35032895
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
 
LVL 8

Expert Comment

by:crysallus
ID: 35032944
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35033012
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
 
LVL 8

Expert Comment

by:crysallus
ID: 35033067
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35033086
Agreed.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 35034401
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 32

Expert Comment

by:phoffric
ID: 35034478
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 35034535
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
 
LVL 1

Author Closing Comment

by:shaolinfunk
ID: 35034772
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35034781
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35034790
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
 
LVL 32

Expert Comment

by:sarabande
ID: 35034791
yes, it is std::pair :)

Sara
0
 
LVL 8

Expert Comment

by:crysallus
ID: 35036284
@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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now