By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

namespace and list problem

P: n/a
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<intit;

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

Mar 12 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
if I try to compile it as above I get :
:7: error: `std::list<int, std::allocator<int' is not a namespace
Why?
My guess std::list<int, std::allocator<int is a class name not a
namespace name. But here some C++ specialist must say something.
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.
Mar 12 '07 #2

P: n/a
* 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<intit;

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<intis a class, not a namespace. You
cannot use 'using' for things in std::list<int>.

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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 12 '07 #3

P: n/a
Alf P. Steinbach wrote:
* Michael:
>: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.
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
Mar 12 '07 #4

P: n/a
On Mar 12, 10:40 am, "Michael" <michael5...@yahoo.comwrote:
using std::list<int>::iterator;
typedef std::list<int>::iterator not_so_cool_idea;

=^)

Mar 12 '07 #5

P: n/a
ja******@gmail.com wrote:
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....24e006d2881a9/

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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 14 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.