namespace and list problem

M

Michael

Hi All,

I've wasted heaps of time today trying to get some code I copied from a text
to compile. I have cut it down to the following couple of lines which
demonstrate the core of the problem:

#include <iostream>
#include <list>
//using namespace std;
using std::cout;
using std::cin;
using std::list;
using std::list<int>::iterator;

int main(){
list<int> myList;

iterator<int> it;

return 0;
}

if I try to compile it as above I get :
:7: error: `std::list<int, std::allocator<int> >' is not a namespace
Why?

if I remove the comment marks from the 'using namespace std' and comment out
all the specific 'using's (to try and get round the above) I get:
:12: error: wrong number of template arguments (1, should be 5)
Where did the 5 instead of 1 come from? <sigh>

Thanks for your help

Michael
 
J

jacekfoo

if I try to compile it as above I get :
:7: error: `std::list<int, std::allocator<int> >' is not a namespace
Why?

if I remove the comment marks from the 'using namespace std' and comment out
all the specific 'using's (to try and get round the above) I get:
:12: error: wrong number of template arguments (1, should be 5)
Where did the 5 instead of 1 come from? <sigh>

#include <iostream>
#include <list>


int main(){
std::list<int> myList;

std::list<int>::iterator it;

return 0;
}

Compiles.

In C++ FAQ lite is quite long entry about why 'using something', and
'using namespace something' is evil.

My rule of the thumb is not to use it. I spend time typing, and I
spend less time finding errors (but I'm not a pro progammer).

Hope it helps.
 
A

Alf P. Steinbach

* Michael:
Hi All,

I've wasted heaps of time today trying to get some code I copied from a text
to compile. I have cut it down to the following couple of lines which
demonstrate the core of the problem:

#include <iostream>
#include <list>
//using namespace std;
using std::cout;
using std::cin;
using std::list;
using std::list<int>::iterator;

int main(){
list<int> myList;

iterator<int> it;

return 0;
}

if I try to compile it as above I get :
:7: error: `std::list<int, std::allocator<int> >' is not a namespace
Why?

A 'using' declaration only applies to something directly in a namespace,
not things in classes. std::list<int> is a class, not a namespace. You
if I remove the comment marks from the 'using namespace std' and comment out
all the specific 'using's (to try and get round the above) I get:
:12: error: wrong number of template arguments (1, should be 5)
Where did the 5 instead of 1 come from? <sigh>

You haven't shown the code producing that error message, but most
probably it's because of your use of 'iterator', which isn't a template.
 
M

Markus Moll

Alf said:
* Michael:

You haven't shown the code producing that error message, but most
probably it's because of your use of 'iterator', which isn't a template.

Yes, it is ;-)
Without all the using directives, the compiler is probably thinking of

namespace std {
template<class Category, class T, class Distance = ptrdiff_t,
class Pointer = T*, class Reference = T&>
struct iterator {
typedef T value_type;
typedef Distance difference_type;
typedef Pointer pointer;
typedef Reference reference;
typedef Category iterator_category;
};
}

Markus
 
M

Marcus Kwok

In C++ FAQ lite is quite long entry about why 'using something', and
'using namespace something' is evil.

Honestly, I disagree with this entry in the FAQ. See this discussion
for more details:
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/c8c24e006d2881a9/

In a nutshell, using directives and using declarations are OK if you can
control the scope in which they're applied. For example, don't use them
in header files, since it will affect anyone who includes your header.
But for localized use in implementation files, they can actually enhance
the readability of the code.

As Daniel T. mentions in the above-cited thread, see "C++ Coding
Standards" by Sutter and Alexandrescu for more information.
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top