C++ Template problem

M

Mat DeLong

I have never been stuck on programming something before to the point I give
up... this is a first. I am programming what should be something very easy
in C++... using Templates. Here is the code, and the error I get:


--------------
namespace LIST {

template <typename T> struct Link;
template <typename T> class List;
template <typename T> class ListIterator;

// helper declarations

template<typename T> bool operator==(const List<T>::template ListIterator&,
const List<T>::template ListIterator&);
template<typename T> bool operator!=(const List<T>::template ListIterator&,
const List<T>::template ListIterator&);

template <typename T> class List {
public:
List();
virtual ~List();
private:
List(const List&);
List<T>& operator=(const List&);
public:
void insert(const T&);

class ListIterator {
friend bool operator== <> (const ListIterator&, const
ListIterator&);
public:
ListIterator(Link<T>* = 0);
ListIterator& operator++();
T& operator*(); // ListIterator i; *i cin >> *i;
const T& operator*() const;
private:
Link<T>* current_;
}; //ListIterator

ListIterator begin() const;
ListIterator end() const;
private: // List
Link<T>* head_;
};

}

---------------------------- Error



Script started on Mon Apr 11 22:34:01 2005
bash-2.05b$ make -f a6.make
g++ -Wall -pedantic -c main.cpp
In file included from main.cpp:1:
list.h:9: error: expected unqualified-id before '&' token
list.h:9: error: expected `,' or `...' before '&' token
list.h:9: error: ISO C++ forbids declaration of `parameter' with no type
list.h:9: error: `bool LIST::eek:perator==(int)' must have an argument of class
or enumerated type
list.h:9: error: `bool LIST::eek:perator==(int)' must take exactly two
arguments
list.h:10: error: expected unqualified-id before '&' token
list.h:10: error: expected `,' or `...' before '&' token
list.h:10: error: ISO C++ forbids declaration of `parameter' with no type
list.h:10: error: `bool LIST::eek:perator!=(int)' must have an argument of
class or enumerated type
list.h:10: error: `bool LIST::eek:perator!=(int)' must take exactly two
arguments
...........
-------------------------------------------------------------------------
Any ideas?



Thanks

Mat
 
A

Alf P. Steinbach

* Mat DeLong:
I have never been stuck on programming something before to the point I give
up... this is a first. I am programming what should be something very easy
in C++... using Templates. Here is the code, and the error I get:

It seems your goal is something simple (is it a list templated on the
element type?), and the road you've chosen incredibly complex.

Have you considered using std::list?

The code presented is, btw., incomplete, e.g. where's that class 'Link'.
 
M

Mat DeLong

Alf P. Steinbach said:
* Mat DeLong:

It seems your goal is something simple (is it a list templated on the
element type?), and the road you've chosen incredibly complex.

Have you considered using std::list?

The code presented is, btw., incomplete, e.g. where's that class 'Link'.

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

Here is the implementation of Link. I didn't supply the .cpp implementation
file because the error is located in the header. You are correct, this is a
simple list with its own iterator, storying template-type elements.

Any help is greatly appreciated.

template <typename T>
struct Link {
Link* next;
T value;
Link(const T& t) { next = 0; value = t; }
};
 
A

Alf P. Steinbach

* Mat DeLong:
Here is the implementation of Link. I didn't supply the .cpp implementation
file because the error is located in the header.

FAQ item 37.5:
You are correct, this is a
simple list with its own iterator, storying template-type elements.

Any help is greatly appreciated.

Start as simple as you can: make it work _without_ any templating.

Then add things.
 
J

John Carson

Mat DeLong said:
I have never been stuck on programming something before to the point
I give up... this is a first. I am programming what should be
something very easy in C++... using Templates. Here is the code, and
the error I get:


--------------
namespace LIST {

template <typename T> struct Link;
template <typename T> class List;
template <typename T> class ListIterator;


Ditch the declaration of ListIterator. You aren't declaring
List::ListIterator, you are declaring an "outer" class called ListIterator.
You cannot forward declare List::ListIterator at namespace scope.
Fortunately the declaration is redundant anyway.

// helper declarations

template<typename T> bool operator==(const List<T>::template
ListIterator&, const List<T>::template ListIterator&);
template<typename T> bool operator!=(const List<T>::template
ListIterator&, const List<T>::template ListIterator&);

Try

template<typename T> bool operator==(const typename List<T>::ListIterator&,
const typename List<T>::ListIterator&);
template<typename T> bool operator!=(const typename List<T>::ListIterator&,
const typename List<T>::ListIterator&);

This will compile, but it probably won't give you what you want in the end.
The problem is that the compiler will not deduce the template parameter T if
you call the operator with

iterator1 == iterator2

This is because (in the general case) Y<T>::X could be the same type for
more than one T type, making the deduction of T impossible.

If you don't want to have to call operator== explicitly and explicitly
supply the template parameter, then you should bypass this template
deduction problem by making operator== a member function (naturally, the
same goes for operator!=).
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top