STL iterators and the std namespace

O

onion_skin

How do I bring STL iterators into the current scope? eg.

#include <vector>
using std::vector;

int main()
{
vector<int>::iterator it;

return 0;
}


gives an error
 
C

Corey Murtagh

onion_skin said:
How do I bring STL iterators into the current scope? eg.

#include <vector>
using std::vector;

int main()
{
vector<int>::iterator it;

return 0;
}


gives an error

What error, and on what compiler? I tried the above on a few different
compilers with no errors. Comeau's online test also compiles it, albeit
with a warning that 'variable "it" is declared but never referenced'.
 
O

onion_skin

What error? The code is legal C++ and compiles fine here.
Perhaps the compiler you're using is not good enough? If
that's so, try adding 'using namespace std;' in the scope
of the 'main' function.

Victor


MSVC6.0 gives the error:

"error C2653: 'vector<int,class std::allocator<int> >' : is not a
class or namespace name"

I don't want to use expose the entire library by using 'using
namespace std' (although that, obviously, works)
 
C

Corey Murtagh

onion_skin said:
MSVC6.0 gives the error:

"error C2653: 'vector<int,class std::allocator<int> >' : is not a
class or namespace name"

That's interesting. Worked fine on VC++ 6.0 when I tried it. Are you
sure you posted enough code to replicate the problem?
 
V

Victor Bazarov

Corey Murtagh said:
That's interesting. Worked fine on VC++ 6.0 when I tried it.

Are you sure? It didn't when I tried it on VC++ v6.0 sp5.
Are you
sure you posted enough code to replicate the problem?

Are you sure _you_ tried the right code?
---------------------------------------------
#include <vector>

using std::vector;

int main()
{
vector<int>::iterator it;
return 0;
}
 
T

Tom

Also compiles without error or warning, using MS Compiler 7.1 (now 97% C++
compliant)

Regards,
Tom
 
C

Corey Murtagh

Victor said:
"Corey Murtagh" <[email protected]> wrote...


Are you sure? It didn't when I tried it on VC++ v6.0 sp5.




Are you sure _you_ tried the right code?

eep... ok, I messed up. Sorry. I guess all those months of typing
'std::' in front of every STL object has gone to my brain.

On the upside, I can confirm that it *does* work on the following compilers:

BCB4
Dev-C++/MinGW32 v2.95.3-6
g++ v2.95.4

Also works on Comeau's online test :)

One work-around for VC++ 6.0:

---------------
#include <vector>
#define vector std::vector

int main()
{
vector<int>::iterator it;

return 0;
}
 
O

onion_skin

Are you sure? It didn't when I tried it on VC++ v6.0 sp5.


Are you sure _you_ tried the right code?
---------------------------------------------
#include <vector>

using std::vector;

int main()
{
vector<int>::iterator it;
return 0;
}


This is the code
-------------------------------------------
#include <vector>
using std::vector;


int main()
{
vector<int>::iterator it;

return 0;
}

-------------------------------------------
 
J

John Harrison

Tom said:
Also compiles without error or warning, using MS Compiler 7.1 (now 97% C++
compliant)

Regards,
Tom

Where do you get 7.1 from? I'm on 7.0.9466 and I reckon I'm only 96%
complaint.

john
 
G

Gavin Deane

Corey Murtagh said:
eep... ok, I messed up. Sorry. I guess all those months of typing
'std::' in front of every STL object has gone to my brain.

On the upside, I can confirm that it *does* work on the following compilers:

BCB4
Dev-C++/MinGW32 v2.95.3-6
g++ v2.95.4

Also works on Comeau's online test :)

One work-around for VC++ 6.0:

---------------
#include <vector>
#define vector std::vector

int main()
{
vector<int>::iterator it;

return 0;
}

A better (because it doesn't use a macro, buit still irritating
because it shouldn't be necessary at all) work-around for VC++ 6.0:

#include <vector>

int main()
{
std::vector<int>::iterator it;

return 0;
}

This VC++ 6.0 feature has got me into the habit of using fully
qualified names in preference to using declarations.

GJD
 

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

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top