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

Templates and Subclasses C++ help

P: n/a
Working on a C++ assignment and I cant figure out the
problems I am having w/ Templates and Subclasses.

I know there are issues with templates and certain compilers
so I am not sure what the problem is exactly.

I am hoping its an easy overlook.

This class is of the linkedLIst, Iterator, and Node modified to use
Templates and so that Iterator and Node are subclasses of List
(linkedlist) as our assignment specifies.

Code is from Big C++ by Horstmann but modified to use templates and
subclassing.

Compiler I am using:
BOrland 5.5.1 win

Also tried mingw 3.4.2 but was told it has problems w/ Templates.
Problem:

I keep getting "Declaration terminated incorrectly" on different
lines.
It seems to only occur on lines that return: "List<T>::Iterator<T>"

So it seems to be an issue with templates and subclasses or I am not
doing it right. Talking to the instructor tomorrow for help but any
help before that would be appreciated. Thanks.

//CODE:

linkedList.cpp:

#include <string>
#include <iostream>
#include <cassert>
#include "linkedList.h"

using namespace std;

// ... CODE HERE WAS CUT OUT FOR BREVITY but my header file is below

template<class T>
List<T>::Iterator<TList<T>::begin() const // PROBLEM WITH THIS
DECLARATION
{
Iterator<Titer;
iter.position = first;
iter.last = last;
return iter;
}

template<class T>
List<T>::Iterator<TList<T>::end() const // PROBLEM WITH THIS
DECLARATION
{
Iterator<Titer;
iter.position = NULL;
iter.last = last;
return iter;
}

// Selection sort: from text book: Big C++ page 549 (modified)

template<typename T>
List<T>::Iterator<TList<T>::min_position(Iterator< Tfrom,
Iterator<Tto)// PROBLEM WITH THIS //DECLARATION
{
Iterator<Tmin_pos = from;//original.begin();
from.next(); // hence i = from.next()
Iterator<Ti = from;
for(; !i.equals(to); i.next())
{
if(i.get() < min_pos.get())
{
cout << " new min_pos! " << endl;
min_pos = i;
}
}
return min_pos;
}
linkedList.h CODE:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <string>
#include <iostream>
#include <cassert>

using namespace std;

template<typename T>
class List
{
friend ostream& operator<<(ostream& out, const List& theList);
private:
template<typename N>
class Node
{
public:
Node(N s);
private:
N data;
Node<N>* previous;
Node<N>* next;
//friend class List;
//friend class Iterator;
};
public:
template<typename U>
class Iterator
{
public:
Iterator();
U get() const;
void set(U s);
void next();
void previous();
bool equals(Iterator<Ub) const;
private:
Node<U>* position;
Node<U>* last;
//friend class List;
};
public:
List();
//copy constructor, = operator overload, destructor, == overload
(this can be order based),
//<< overload:
List(const List<T>& original);
List& operator=(const List<T>& original);
~List();
bool operator==(const List<T>& rhs) const;
// end my methods
void push_back(T s);
void insert(Iterator<Titer, T s);
Iterator<Terase(Iterator<Ti);
Iterator<Tbegin() const;
Iterator<Tend() const;
void print();
void selection_sort();
private:
void init(Node<T>* value);
Node<T>* first;
Node<T>* last;
Iterator<Tmin_position(Iterator<Tfrom, Iterator<Tto);
void swap(Iterator<T>& x, Iterator<T>& y);
};
#endif

May 29 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ryanoasis wrote:
Working on a C++ assignment and I cant figure out the
problems I am having w/ Templates and Subclasses.

I know there are issues with templates and certain compilers
so I am not sure what the problem is exactly.

I am hoping its an easy overlook.
Problem:

I keep getting "Declaration terminated incorrectly" on different
lines.
It seems to only occur on lines that return: "List<T>::Iterator<T>"
template<class T>
List<T>::Iterator<TList<T>::begin() const // PROBLEM WITH THIS
DECLARATION
template<class T>
typename List<T>::Iterator<TList<T>::begin() const

--
Ian Collins.
May 29 '07 #2

P: n/a
No dice. I just tried that. Same error msg when I compile.

THank you for the quick reply though. I was hopeful that would have
solved it.
May 29 '07 #3

P: n/a
ryanoasis wrote:
No dice. I just tried that. Same error msg when I compile.
For what? Please retain enough context from the message you are
replying to for your response to make sense on its own.
THank you for the quick reply though. I was hopeful that would have
solved it.
I should have looked a bit closer, you should probably remove the
template type and all uses of it from Iterator and note, replace it with T.

class Node
{
public:
Node(T s);
private:
T data;
Node* previous;
Node* next;
//friend class List;
//friend class Iterator;
};
public:
class Iterator
{
public:
Iterator();
T get() const;
void set(T s);
void next();
void previous();
bool equals(Iterator b) const;
private:
Node* position;
Node* last;
//friend class List;
};

....

Iterator begin() const;

....

template<class T>
typename List<T>::Iterator List<T>::begin() const

--
Ian Collins.
May 29 '07 #4

P: n/a
Thank you for your help.

Your suggestion paid off and even though I ran into further problems
I was on enough of a roll to solve them. By no means do I fully
understand Templates and I
can see how they can be confusing.

Your help is much appreciated.

May 30 '07 #5

P: n/a
ryanoasis wrote:
Thank you for your help.
You really should keep some of the message you are replying to.
Your suggestion paid off and even though I ran into further problems
I was on enough of a roll to solve them. By no means do I fully
understand Templates and I
can see how they can be confusing.
One more thing to bear in mind, in the context of your question the
correct term would be "nested classes".

--
Ian Collins.
May 30 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.