473,791 Members | 3,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templates and Subclasses C++ help

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 "Declaratio n terminated incorrectly" on different
lines.
It seems to only occur on lines that return: "List<T>::Itera tor<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>::Iterat or<TList<T>::be gin() const // PROBLEM WITH THIS
DECLARATION
{
Iterator<Titer;
iter.position = first;
iter.last = last;
return iter;
}

template<class T>
List<T>::Iterat or<TList<T>::en d() 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<typena me T>
List<T>::Iterat or<TList<T>::mi n_position(Iter ator<Tfrom,
Iterator<Tto)// PROBLEM WITH THIS //DECLARATION
{
Iterator<Tmin_p os = 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<typena me T>
class List
{
friend ostream& operator<<(ostr eam& out, const List& theList);
private:
template<typena me N>
class Node
{
public:
Node(N s);
private:
N data;
Node<N>* previous;
Node<N>* next;
//friend class List;
//friend class Iterator;
};
public:
template<typena me 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==(cons t 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_p osition(Iterato r<Tfrom, Iterator<Tto);
void swap(Iterator<T >& x, Iterator<T>& y);
};
#endif

May 29 '07 #1
5 3158
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 "Declaratio n terminated incorrectly" on different
lines.
It seems to only occur on lines that return: "List<T>::Itera tor<T>"
template<class T>
List<T>::Iterat or<TList<T>::be gin() const // PROBLEM WITH THIS
DECLARATION
template<class T>
typename List<T>::Iterat or<TList<T>::be gin() const

--
Ian Collins.
May 29 '07 #2
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
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>::Iterat or List<T>::begin( ) const

--
Ian Collins.
May 29 '07 #4
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2900
by: Thomas Philips | last post by:
I'm teaching myself programming using Python, and have a question about subclasses. My game has two classes, Player and Alien, with identical functions, and I want to make Player a base class and Alien a derived class. The two classes are described below class Player(object): #Class attributes for class Player threshold = 50 n=0 #n is the number of players
0
1308
by: Vera | last post by:
Hi, I have a very annoying problem, with which I NEED HELP DESPERATELY!! It smells like a bug to me, but I'm not sure. SITUATION This description is a very much simplified version of the real situation. I have the following class structure: ASSEMBLY ATools
2
2173
by: kelvSYC | last post by:
I'm trying to program something along the lines of a "trading card" idea: I have a single copy of the "card" in the program, yet there may be multiple "instances" of the "card", with differing information (such as the owner of the "card") in each instance. struct Person; Person Bob; Person Joe;
4
4803
by: ro86 | last post by:
Hello everyone! I am a newbie to C++ (~1 Week experience) and I have a few months of experience with object-oriented languages (Objective-C). I am currently working just for fun on a particle system. All the particles are controlled by a "server". The server performs all kinds of operations on them (updating, drawing etc.). The particles (my "clients") on the other hand need to retrieve once a while some information from their server...
2
4420
by: MR | last post by:
I'm hoping that someone here will be able to direct me to some litrature on how to get a list of a classes subclasses. I.e. need the inherited class to know about subclasses that are inherited form it. I guess that I'll have to use reflection but I can't find any examples on MSDN. Thanks in advance, Mark
5
2963
by: dj | last post by:
I somehow understand why member function templates cannot be virtual, but my question is how I could achieve something similar to this: class A { public: template<typename Tvirtual f() = 0; } class B : public A { public:
5
2043
by: Zeppe | last post by:
Hi all! my problem is this one, I think that it could be a common one, maybe a pattern, so if you can help me somehow it would be great. Let's suppose I have a class Base class Base { // impl. };
14
4083
by: Marko | last post by:
I have abstract superclass named Element, and several subclasses derived from it: And, Or, Nand, Nor... I have method which has to take 2 subclasses of element as its parameters types. Can't use void myFunction(Element& e1,Element& e2); prototype because I am trying to pass it objects of class And or any other subclass. Need help..
10
2283
by: Karlo Lozovina | last post by:
Hi, what's the best way to keep track of user-made subclasses, and instances of those subclasses? I just need a pointer in a right direction... thanks. -- Karlo Lozovina -- Mosor
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.