Templates and Subclasses C++ help

R

ryanoasis

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<T> List<T>::begin() const // PROBLEM WITH THIS
DECLARATION
{
Iterator<T> iter;
iter.position = first;
iter.last = last;
return iter;
}

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

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

template<typename T>
List<T>::Iterator<T> List<T>::min_position(Iterator<T> from,
Iterator<T> to)// PROBLEM WITH THIS //DECLARATION
{
Iterator<T> min_pos = from;//original.begin();
from.next(); // hence i = from.next()
Iterator<T> i = 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<U> b) 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<T> iter, T s);
Iterator<T> erase(Iterator<T> i);
Iterator<T> begin() const;
Iterator<T> end() const;
void print();
void selection_sort();
private:
void init(Node<T>* value);
Node<T>* first;
Node<T>* last;
Iterator<T> min_position(Iterator<T> from, Iterator<T> to);
void swap(Iterator<T>& x, Iterator<T>& y);
};


#endif
 
I

Ian Collins

ryanoasis said:
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<T> List<T>::begin() const // PROBLEM WITH THIS
DECLARATION

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

ryanoasis

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.
 
I

Ian Collins

ryanoasis said:
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
 
R

ryanoasis

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.
 
I

Ian Collins

ryanoasis said:
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".
 

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
474,262
Messages
2,571,045
Members
48,769
Latest member
Clifft

Latest Threads

Top