473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

C++ Template problem

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
4 2562
* 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

"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
* 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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
3
by: David Komanek | last post by:
Hi all, I am trying to learn more about how to use g++/Cygwin to produce dll files on WinXP. And I have a problem which at the first look seems to be an obvious dll-export problem, but I don't...
5
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something...
2
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.