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

C++ Template problem

P: n/a
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::operator==(int)' must have an argument of class
or enumerated type
list.h:9: error: `bool LIST::operator==(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::operator!=(int)' must have an argument of
class or enumerated type
list.h:10: error: `bool LIST::operator!=(int)' must take exactly two
arguments
...........
-------------------------------------------------------------------------
Any ideas?

Thanks

Mat
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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'.

--
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?
Jul 23 '05 #2

P: n/a

"Alf P. Steinbach" <al***@start.no> wrote in message
news:42*****************@news.individual.net...
* 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'.

--
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; }
};
Jul 23 '05 #3

P: n/a
* 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:
<url: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.7>.

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.

--
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?
Jul 23 '05 #4

P: n/a
"Mat DeLong" <05*****@acadiau.ca> wrote in message
news:d3**********@poseidon.acadiau.ca
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!=).
--
John Carson

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.