You are not logged in.

#1 2006-01-05 19:35:52

Neuro
Member
From: Poland
Registered: 2005-10-12
Posts: 352

STL on C++

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 sad

#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

#2 2006-01-05 20:03:47

xaos5
Member
Registered: 2005-12-30
Posts: 75

Re: STL on C++

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

#3 2006-01-05 20:12:33

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: STL on C++

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

#4 2006-01-05 21:03:11

Neuro
Member
From: Poland
Registered: 2005-10-12
Posts: 352

Re: STL on C++

Cerebral wrote:

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 smile Thanks!

Offline

#5 2006-01-06 21:45:48

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: STL on C++

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

Board footer

Powered by FluxBB