STL question

Discussion in 'C++' started by foolsmart2005@gmail.com, May 28, 2008.

  1. Guest

    I use dev C++ to write my program,
    it occurs error, what does it mean, the program is ok running in VC++,
    but cannot in Dev C++.

    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;

    16 E:\Dev C++\stl001.cpp missing template arguments before "p"
    , May 28, 2008
    #1
    1. Advertising

  2. On Wed, 28 May 2008 16:34:14 +0200,
    <> wrote:

    > I use dev C++ to write my program,
    > it occurs error, what does it mean, the program is ok running in VC++,
    > but cannot in Dev C++.
    >
    > 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;
    >
    > 16 E:\Dev C++\stl001.cpp missing template arguments before "p"



    In the STL, you have not classes directely named iterator.
    However, you have in the most of standard container, an special acces
    named iterator.

    The syntax to have acces at it is:
    container<type>::iterator p.

    So in tour case it is :
    vector<int>::iterator p;

    David Côme.
    David Côme, May 28, 2008
    #2
    1. Advertising

  3. David Côme wrote:
    > In the STL, you have not classes directely named iterator.
    > However, you have in the most of standard container, an special acces
    > named iterator.
    >
    > The syntax to have acces at it is:
    > container<type>::iterator p.
    >
    > So in tour case it is :
    > vector<int>::iterator p;


    In the upcoming C++ standard you can also do it more easily like this:

    auto p = container.begin(); // or whatever
    Juha Nieminen, May 28, 2008
    #3
  4. bilgekhan Guest

    <> wrote:
    >
    > I use dev C++ to write my program,
    > it occurs error, what does it mean, the program is ok running in VC++,
    > but cannot in Dev C++.


    I would say you lie. :)
    This code cannot compile on any compiler.

    > 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;
    >
    > 16 E:\Dev C++\stl001.cpp missing template arguments before "p"


    See the other replies.
    It must be vector<int>::iterator p;
    bilgekhan, May 28, 2008
    #4
  5. James Kanze Guest

    On May 28, 6:53 pm, "bilgekhan" <>
    wrote:
    > <> wrote:


    > > I use dev C++ to write my program, it occurs error, what
    > > does it mean, the program is ok running in VC++, but cannot
    > > in Dev C++.


    > I would say you lie. :)
    > This code cannot compile on any compiler.


    > > 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;


    > > 16 E:\Dev C++\stl001.cpp missing template arguments before "p"


    > See the other replies.
    > It must be vector<int>::iterator p;


    Certainly not what was wanted, but there is a class template
    std::iterator. (In fact, it only makes sense as a base class
    for a real iterator, and you'd never declare an instance of it.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 28, 2008
    #5
  6. Hi!

    Juha Nieminen wrote:
    > In the upcoming C++ standard you can also do it more easily like this:
    >
    > auto p = container.begin(); // or whatever


    While convenient, type inference has always been a source of bugs and
    dubious runtime errors in other languages that rely on it (like
    JavaScript or Perl). So I would recommend not to use it if the return
    type of an expression is well known.
    However, there are cases in generic programming where you won't come
    around it. Lambda expressions are an example.


    Marcel
    Marcel Müller, May 28, 2008
    #6
  7. Guest

    On May 28, 10:34 pm, ""
    <> wrote:
    > I use dev C++ to write my program,
    > it occurs error, what does it mean, the program is ok running in VC++,
    > but cannot in Dev C++.
    >
    > 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;
    >
    > 16 E:\Dev C++\stl001.cpp missing template arguments before "p"


    the above is only part of the code , the whole code is as follows,
    #include <iostream>
    #include <vector>
    using namespace std;
    // using 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;

    cout << "Setting entries to 0:\n";
    for(p = container.begin();p!=container.end();p++)
    *p = 0;

    cout <<"Container now contains:\n";
    for(p = container.begin();p!=container.end();p++)
    cout << *p << " ";

    cout << endl;

    return 0;
    }

    copied from the book <<Absolute C++>>, but I cannot compile, why?
    , May 29, 2008
    #7
  8. Marcel Müller wrote:

    > While convenient, type inference has always been a source of bugs and
    > dubious runtime errors in other languages that rely on it (like
    > JavaScript or Perl).


    Type inference is something different from value typing (or
    dynamic/manifest/runtime typing), which you seem to talk about.
    Matthias Buelow, May 29, 2008
    #8
  9. gpderetta Guest

    On May 28, 11:12 pm, Marcel Müller <>
    wrote:
    > Hi!
    >
    > Juha Nieminen wrote:
    > >   In the upcoming C++ standard you can also do it more easily like this:

    >
    > > auto p = container.begin(); // or whatever

    >
    > While convenient, type inference has always been a source of bugs and
    > dubious runtime errors in other languages that rely on it (like
    > JavaScript or Perl). So I would recommend not to use it if the return
    > type of an expression is well known.


    It will still be statically checked. C++0x auto has nothing to do with
    dynamically typed variables in languages like Perl. Do you have a (non
    contrived) example of static type inference causing runtime errors?

    --
    pgd
    gpderetta, May 29, 2008
    #9
  10. Triple-DES Guest

    On 29 Mai, 12:26, "" <>
    wrote:
    > On May 28, 10:34 pm, ""
    >
    > <> wrote:
    > > I use dev C++ to write my program,
    > > it occurs error, what does it mean, the program is ok running in VC++,
    > > but cannot in Dev C++.

    >
    > >   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;

    >
    > > 16 E:\Dev C++\stl001.cpp missing template arguments before "p"

    >
    > the above is only part of the code , the whole code is as follows,
    > #include <iostream>
    > #include <vector>
    > using namespace std;
    > // using 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;
    >
    >     cout << "Setting entries to 0:\n";
    >     for(p = container.begin();p!=container.end();p++)
    >           *p = 0;
    >
    >     cout <<"Container now contains:\n";
    >     for(p = container.begin();p!=container.end();p++)
    >           cout << *p << " ";
    >
    >     cout << endl;
    >
    >     return 0;
    >
    > }
    >
    > copied from the book <<Absolute C++>>, but I cannot compile, why?


    In line 16, you should be using
    vector<int>::iterator, not just iterator.

    DP
    Triple-DES, May 29, 2008
    #10
  11. Jerry Coffin Guest

    In article <a1bbefdf-903f-4fae-83a5-1272298e0393
    @q27g2000prf.googlegroups.com>, says...

    [ ... ]

    > // using vector<int>::iterator;


    Here's (probably) the source of the problem: this wasn't originally
    commented out.

    > 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;


    The original intent (with the previous line still "executable") was that
    this refer to 'std::vector<int>::iterator'. With the previous line
    commented out, it refers to 'std::iterator', which doesn't work.

    > copied from the book <<Absolute C++>>, but I cannot compile, why?


    I'd say "because you ended up with a lousy book. Even when/if you fix
    the code so it compiles, I'd consider it a long ways from exemplary
    code. If I was going to do what it does, I'd write it something like
    this:

    #include <iostream>
    #include <vector>

    void show(std::vector<int> const &container) {
    std::copy(container.begin(), container.end(),
    std::eek:stream_iterator<int>(std::cout, " "));
    std::cout << "\n";
    }

    int main()
    {
    std::vector<int> container;
    for(int i=1;i<=4;i++)
    container.push_back(i);

    std::cout << "Here is what is in the container:\n";
    show(container);

    std::cout << "Setting entries to 0:\n";
    std::fill_n(container.begin(), container.size(), 0);

    std::cout <<"Container now contains:\n";
    show(container);

    return 0;
    }

    As a generalization, I'd say almost anytime you have something like:

    some_type::iterator x;
    for (x=container.begin(); x!=container.end(); ++x)
    do_something();

    You've _probably_ made a mistake. You should probably be using an
    algorithm instead. In this case, he duplicated two algorithms already in
    the standard library: std::fill_n and std::copy. Far better to use than
    duplicate them. Another principle is often phrased as "don't repeat
    yourself" -- in the previous code, he had two repetitions of identical
    code to show the contents of the container. I've moved that into a
    function of its own.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, May 29, 2008
    #11
  12. Guest

    On May 28, 10:34 pm, ""
    <> wrote:
    > I use dev C++ to write my program,
    > it occurs error, what does it mean, the program is ok running in VC++,
    > but cannot in Dev C++.
    >
    > 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;
    >
    > 16 E:\Dev C++\stl001.cpp missing template arguments before "p"


    The problem has been solved, thanks everyone.
    , May 29, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Bruce

    To STL or not to STL

    Allan Bruce, Oct 16, 2003, in forum: C++
    Replies:
    41
    Views:
    1,043
    Christopher Benson-Manica
    Oct 17, 2003
  2. Replies:
    4
    Views:
    798
    Daniel T.
    Feb 16, 2006
  3. Replies:
    2
    Views:
    552
    klaus hoffmann
    Feb 22, 2006
  4. Replies:
    5
    Views:
    503
    Markus Schoder
    Apr 16, 2006
  5. Steve
    Replies:
    2
    Views:
    503
    Andre Kostur
    Nov 6, 2007
Loading...

Share This Page