0

I am new to c++, and I am trying to get a basic program to initialize a list of short unsigned integers. I am compiling and running using scygwin and g++.

Below is the code in the .cpp file:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <typeinfo>
using namespace std;


int main (int argc, char* argv[]) {

list<int> events;

return 0;
}

which I run by typing the following command into cygwin terminal:

$ g++ -o test.out test.cpp

However, I get the following compilation errors:

test.cpp: In function ‘int main(int, char**)’: test.cpp:16:1: error: ‘list’ was not declared in this scope list events;
^ test.cpp:16:6: error: expected primary-expression before ‘int’ list events; ^

I am confused about why list is not in the scope, since I am using namespace std? I found a similar question asked about this on a c++ forum, but my problem would be resolved with that. Anyone know what the problem is here?

-Paul

Paul
  • 1,106
  • 1
  • 16
  • 39
  • 9
    #include – AndyG Jun 24 '16 at 21:23
  • thank you. but why isn't that automatically included when I do `using namespace std`? – Paul Jun 24 '16 at 21:26
  • 1
    When you #include a file, it's like a literal copy paste. You get a lot of items that are hidden within namespace std. When you say `using namespace std;` you're not actually importing anything, but rather just saving yourself the trouble of having to qualify items within that namespace. – AndyG Jun 24 '16 at 21:36
  • just to be clear, you're saying that is hidden within namespace std until I explicitly include it? I guess I have been assuming that there is always an implicit `#include ` in every c++ file, which would give you access to anything in that namespace. – Paul Jun 24 '16 at 21:40
  • The namespace isn't a collection of code. It is simply a naming label that helps organize code. See my updated answer – Assimilater Jun 24 '16 at 21:42

1 Answers1

4

using namespace std; doesn't add any functionality to your code. It just means you don't have to type std:: when referencing things in the std namespace, like std::list.

To actually include the code base for std::list into your program, you need to add:

#include <list>

When in doubt about this kind of thing, doing a google search for cpp reference list will turn up a page like this where you can see: Defined in header <list> at the top.

Here's another question about using namespace std; that may prove useful and why you shouldn't use it. I'll add a little bit to perhaps explain namespaces.

It is common in C++ programs to organize functions into classes and namespaces. Imagine you wrote your own list class to handle certain scenarios. In order to prevent naming conflicts you would put it in a different namespace than std.

namespace MyApp {
    class list;
    void sort(list&);
}

For the majority of a large code base you might still prefer to use std::list but you need MyApp::list for some things. Using namespaces you can cluster your code and prevent naming conflicts for similar functionality.

Summary

using namespace std; makes it so that if you reference a function or class not in the global namespace it looks for it in the std namespace.

#include <list> actually inserts prototypes (information about how to access the code) in your source file during the preprocessor stage.

Community
  • 1
  • 1
Assimilater
  • 944
  • 14
  • 33
  • ok, that makes sense. But I still don't understand why, if list is in the std library, I still have to explicitly include it? – Paul Jun 24 '16 at 21:29
  • 1
    all of your includes are in the standard library, the idea is you only include what you need – kmdreko Jun 24 '16 at 21:34
  • @Paul I've added a little more content to hopefully give you a better picture of what `using namespace` does and what `#include ` does – Assimilater Jun 24 '16 at 21:41
  • great, and thank you for the explanation. Actually, I think my main confusion was even simpler, and was answered finally by what @vu1p3n0x said. I didn't realize all packages I am usually including are in the std library, so I thought `#include` was more like an import. – Paul Jun 24 '16 at 21:44
  • If you're coming from a java/c# background I believe the correlation between `using` in the two languages is quite similar. `#include` is closer to java's `import`. With C# I'm unaware of anything like `import`/`#include`. The closest I can think of is including other libraries in the linking stage of a C# app. – Assimilater Jun 24 '16 at 21:48