I got one compliation error.

N

news.coderaka

I got a compliation error when compiling this programs using g++ 3.4.2.
Who can tell me why?
Thank you.

#include<iostream>
using namespace std;

template<class T>
class list{
class iterator{
friend class list;
iterator operator++(){
return *this;
}
};
};

template<class T>
list<T>::iterator list<T>::iterator::eek:perator++(){
return *this;
}

int main(){
return 0;
}
 
P

petermblair

Well, start off by implementing the correct operator++.

you need:
iterator operator++(int)

for postfix incrementing.
 
T

Thomas Tutone

Well, start off by implementing the correct operator++.

you need:
iterator operator++(int)

for postfix incrementing.

That makes no sense. There is no requirement that you implement both
prefix and postfix increment.

Best regards,

Tom
 
T

Thomas Tutone

I got a compliation error when compiling this programs using g++ 3.4.2.
Who can tell me why?

You would have gotten more timely help if you actually posted the
error. Anyway, your answer is below.
#include<iostream>
using namespace std;

template<class T>
class list{
class iterator{
friend class list;
iterator operator++(){
return *this;
}

In the above few lines, you define operator++() for the list::iterator
class.

And in the lines below you again define operator++(). This violates
the one-definition rule. Define in one place or the other - not both.
template<class T>
list<T>::iterator list<T>::iterator::eek:perator++(){
return *this;
}

Best regards,

Tom
 
M

Marcus Kwok

I got a compliation error when compiling this programs using g++ 3.4.2.
Who can tell me why?
Thank you.

using namespace std;

template<class T>
class list{
[snip]

Name collision?
 
J

Jaspreet

Marcus said:
I got a compliation error when compiling this programs using g++ 3.4.2.
Who can tell me why?
Thank you.

using namespace std;

template<class T>
class list{
[snip]

Name collision?

I guess name collision with std::list would come up only if OP would
#include <list>.
 
O

Old Wolf

Jaspreet said:
I guess name collision with std::list would come up only if OP would
#include <list>.

Any header is allowed to include any other header in C++, so
perhaps <list> has effectively been included already.

But there should be no name collision because std::list is in
the std namespace (the 'using' directive does not change this),
and the new list is in the global namespace.
 
M

Marcus Kwok

Old Wolf said:
Any header is allowed to include any other header in C++, so
perhaps <list> has effectively been included already.

But there should be no name collision because std::list is in
the std namespace (the 'using' directive does not change this),
and the new list is in the global namespace.

However, if <list> were implicitly included, then the using directive
(as I understand it) would effectively insert the name "list" into the
global namespace, causing a collision when an entity of type "list" is
declared.

I do agree that this is unlikely since the OP did not #include <list> (a
simple oversight on my part; I just saw the class named "list" and the
name collision was the first thing that popped into my head).
 
O

Old Wolf

Marcus said:
However, if <list> were implicitly included, then the using directive
(as I understand it) would effectively insert the name "list" into the
global namespace, causing a collision when an entity of type "list" is
declared.

The using directive says to add this namespace to the search path
when an unqualified name is looked up. It does not "import"
anything, and it doesn't add anything to the global namespace.

Here's a program:

#include <list>
#include <iostream>
using namespace std;

template<typename T>
struct list {
list() { std::cout << "Our list" << std::endl; }
};

int main()
{
std::list<int> b;
::list<int> a; // "Our list"
list<int> c; // Error
}

The last line is an error because 'list' is ambiguous.
(In fact my compiler says "'list' undeclared"; I suppose
this is a compiler bug).
 
M

Marcus Kwok

Old Wolf said:
#include <list>
#include <iostream>
using namespace std;

template<typename T>
struct list {
list() { std::cout << "Our list" << std::endl; }
};

int main()
{
std::list<int> b;
::list<int> a; // "Our list"
list<int> c; // Error
}

The last line is an error because 'list' is ambiguous.
(In fact my compiler says "'list' undeclared"; I suppose
this is a compiler bug).

OK, yes, this last situation was what I was talking about. The first
two lines are explicitly qualified, so of course they are fine.
However, I would say that if someone uses "using namespace std;" then
they usually omit the "std::" in declarations (that's the point of it,
right?). Sorry if I mis-worded my previous post.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top