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