STL question

F

foolsmart2005

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"
 
D

David Côme

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.
 
J

Juha Nieminen

David said:
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
 
B

bilgekhan

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

James Kanze

I would say you lie. :)
This code cannot compile on any compiler.
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.)
 
M

Marcel Müller

Hi!

Juha said:
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
 
F

foolsmart2005

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?
 
M

Matthias Buelow

Marcel said:
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.
 
G

gpderetta

Hi!




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?
 
T

Triple-DES

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
 
J

Jerry Coffin

[ ... ]
// 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
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.
 
F

foolsmart2005

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top