A newbie question about STL under GCC

This is a discussion on A newbie question about STL under GCC within the C++ Programming forums, part of the General Programming Boards category; Hello, I am learning STL using G++ under Ubuntu. Today I copied following C++ code from a text book (Absolute ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    1

    A newbie question about STL under GCC

    Hello,

    I am learning STL using G++ under Ubuntu. Today I copied following C++ code from a text book (Absolute C++) and tried to run it:

    Code:
    #include <iostream>
    #include <vector>
    using std::cout;
    using std::endl;
    using std::vector;
    using std::vector<int>::iterator;
    
    int main()
    {
    vector<int> container;
    for (int i=1;i<=4;i++)
    container.push_back(i);
    
    cout << "Here is what is in the container:\n";
    iterator p;
    for (p=container.begin();p!=container.end();p++)
    cout << *p << " ";
    cout << endl;
    }
    When I compile it, got following error:

    STL_vector.cpp:6: error: ‘std::vector<int, std::allocator<int> >’ is not a namespace

    So, what's the problem of: using std::vector<int>::iterator;

    Sorry for this kind of newbie question. Thanks in advance for your help.

    regards,
    Derek
    Reply With Quote

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Quote Originally Posted by featips View Post
    Hello,

    I am learning STL using G++ under Ubuntu. Today I copied following C++ code from a text book (Absolute C++) and tried to run it:

    Code:
    #include <iostream>
    #include <vector>
    using std::cout;
    using std::endl;
    using std::vector;
    using std::vector<int>::iterator;
    
    int main()
    {
    vector<int> container;
    for (int i=1;i<=4;i++)
    container.push_back(i);
    
    cout << "Here is what is in the container:\n";
    iterator p;
    for (p=container.begin();p!=container.end();p++)
    cout << *p << " ";
    cout << endl;
    }
    When I compile it, got following error:

    STL_vector.cpp:6: error: ‘std::vector<int, std::allocator<int> >’ is not a namespace

    So, what's the problem of: using std::vector<int>::iterator;

    Sorry for this kind of newbie question. Thanks in advance for your help.

    regards,
    Derek
    Reply With Quote
    "Quote"
    [ubuntu] GCC question - Ubuntu Forums

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    lol cyberfish....

    Anyway, iterator isn't a namespace that's why. Maybe it meant "typedef std::vector<int>::iterator iterator" not "using std::vector<int>::iterator"
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Quote Originally Posted by featips View Post
    Hello,

    I am learning STL using G++ under Ubuntu. Today I copied following C++ code from a text book (Absolute C++) and tried to run it:

    Code:
    #include <iostream>
    #include <vector>
    using std::cout;
    using std::endl;
    using std::vector;
    using std::vector<int>::iterator;
    
    int main()
    {
    vector<int> container;
    for (int i=1;i<=4;i++)
    container.push_back(i);
    
    cout << "Here is what is in the container:\n";
    iterator p;
    for (p=container.begin();p!=container.end();p++)
    cout << *p << " ";
    cout << endl;
    }
    When I compile it, got following error:

    STL_vector.cpp:6: error: ‘std::vector<int, std::allocator<int> >’ is not a namespace

    So, what's the problem of: using std::vector<int>::iterator;

    Sorry for this kind of newbie question. Thanks in advance for your help.

    regards,
    Derek
    Reply With Quote
    The way that the 'using' syntax works, is that you can specify something without qualifying what *namespace* it exists in. The namespace can be nested within another namespace, such as foo::bar::baz. So basically:

    using namespace NAMESPACE;
    using NAMESPACE::class;
    using NAMESPACE::object;

    Code:
    using std::cout;
    using std::endl;
    using std::vector;
    
    int main()
    {
        typedef vector<int> container_t;
        typedef container_t::iterator iterator_t;
        container_t container;
        for (int i=1;i<=4;i++)
            container.push_back(i);
        cout << "Here is what is in the container:\n";
        iterator_t p;
        for (p=container.begin();p!=container.end();p++)
            cout << *p << " ";
        cout << endl;
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL preformance question
    By l2u in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2006, 04:36 PM
  2. Total Newbie Question
    By Kid A in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2006, 05:36 AM
  3. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 11:13 AM
  4. gcc or g++ Question
    By TK in forum Linux Programming
    Replies: 3
    Last Post: 07-17-2002, 10:36 AM
  5. Newbie question about registries/files
    By SirDavidGuy in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2002, 08:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21