Wednesday, August 21, 2019
c++ - 'list' was not declared in this scope
Answer
Answer
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
#include
#include
#include
#include
using namespace std;
int main (int argc, char* argv[]) {
list 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
Answer
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
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 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 actually inserts prototypes (information about how to access the code) in your source file during the preprocessor stage.
hard drive - Leaving bad sectors in unformatted partition?
Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...
-
My webapp have javascript errors in ios safari private browsing: JavaScript:error undefined QUOTA_EXCEEDED_ERR:DOM Exception 22:An...
-
I would like to use the jquery autocomplete function on a field of a form collection. For example, I have a form collection that got those f...
-
Current setup Microsoft Deployment Toolkit (MDT) 2013 Update 1 (recently released version) All images done from Windows 10 Enterprise machin...
No comments:
Post a Comment