You are not logged in.
Pages: 1
Well I have this strange problem. Before, I used to include the STL C++ headers by doing for exmaple #include <map>. Now it seems not to work
#include <list>
#include <iostream>
list<int> List;
list<int>::iterator iL;
int main()
{
List.push_back(5);
List.push_back(7);
List.push_back(9);
List.push_back(4);
List.push_back(3);
List.push_back(2);
for (iL = List.begin(); iL != List.end(); iL++)
{
cout << *iL;
}
return 0;
}
Whenever I try to compile this using g++ list.cpp -o list I get:
list.cpp:6: error: expected constructor, destructor, or type conversion before '<' token
list.cpp:7: error: expected constructor, destructor, or type conversion before '<' token
list.cpp: In function 'int main()':
list.cpp:11: error: 'List' was not declared in this scope
list.cpp:18: error: 'iL' was not declared in this scope
list.cpp:20: error: 'cout' was not declared in this scope
Everything works fine if I put:
#include <list.h>
#include <iostream.h>
However during compilation it says:
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.0.3/../../../../include/c++/4.0.3/backward/list.h:59,
from list.cpp:2:
/usr/lib/gcc/i686-pc-linux-gnu/4.0.3/../../../../include/c++/4.0.3/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
I'm confused. Anyone could help me out?
Offline
I'm just starting to learn c++ but it seems like
#include <xxx.h> is depriciated and you should use #include <xxx>
is list part of the 32 headers found in section 17.4.1.2 of the c++ standard?
Offline
STL stuff is part of the std namespace.
Either reference it via:
std::list<int> List;
std::list<int>::iterator iL;
or stick
using namespace std;
after your includes.
Offline
STL stuff is part of the std namespace.
Either reference it via:
std::list<int> List; std::list<int>::iterator iL;
or stick
using namespace std;
after your includes.
<bangs his head against the wall> I had a feeling I'm forgeting something Thanks!
Offline
Just for the record, "STL" is a misnomer, from the pre-standard C++ days... it's more proper to refer to it as the "standard library"... but I'm just being picky.
And, if you want to be fancy:
std::list<int> mylist;
//fill mylist
std::copy(mylist.begin(), mylist.end(), std::ostream_iterator(std::cout, ' '), std::ostream_iterator());
Will dump the whole collection. It's a tad more efficient and less error prone. Because it's a generic algorithm, it works with an forward iterable interface.
Offline
Pages: 1