LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2006, 05:18 PM   #1
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
trouble making a [simple] templated node-based stack


ill keep it simple. heres my Stack.h (portion):

Code:
template <class DT>
struct Node
{
	DT data;	
	Node* pUnder;
};

template<class DT>
class Stack
{
private:  // <--- compiler error points here
	Node* pTop;
public:
	Stack();
	~Stack();
	DT pop();
	bool add(DT);
	DT peek();
	void clear();
	int getSize();
};
error in dev C++ is:
- ISO C++ forbids declaration of `Node' with no type
- expected `;' before '*' token

i havent done any templated list-type before.. so im sure its a simple syntax error im doing.

thanks for any help
 
Old 02-14-2006, 05:28 PM   #2
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
I'm a C programmer, so c++ may have changed this, but you probably need to change:
Code:
Node* pTop;
to
Code:
struct Node* pTop;
and
Code:
Node* pUnder;
to
Code:
struct Node* PUnder;
That should clear up the errors. It's common to typedef structure pointers to avoid this typing.

Code:
typedef struct Node * NodePtr;
Or something like that... they you could just type:
Code:
NodePtr PUnder;
 
Old 02-14-2006, 05:34 PM   #3
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
in the past when i have made other lists (or queue, stack, etc) i didnt have to do what you said. the problem i have is that im making one that is templated, so the data in the node can be anything specified at initial creation of the object (of type Stack)

but, i tried what u did (ie struct Node* pTop) and it now points to the line after the line i said was the error in the 1st post, and says: 'template argument required for `struct Node' '

im almost certain its that im missing a 'template <class DT>' or some variant of that (or have too many!) somewhere.. but iv never made a class like this before so i dont know how it SHOULD be.

thanks
 
Old 02-14-2006, 05:41 PM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Is this just an exercise? if not why not use std::stack which is a template already.
Try something like:
Code:
template <class DT>
struct Node
{
	DT data;	
	Node<DT>* pUnder;
};

template<class DT>
class Stack
{
private:  // <--- compiler error points here
	Node<DT>* pTop;
public:
	Stack();
	~Stack();
	DT pop();
	bool add(DT);
	DT peek();
	void clear();
	int getSize();
};
also are you putting function definitions for the template class in the header file? thats where they need to go.
 
Old 02-14-2006, 05:52 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
thanks dmail!

i tried putting the 'Node<DT>*' in afew combinations (from the 2 that u said) and it seems like it only needs to be used in the 2nd one, ie Node<DT>* pTop. the first one (in struct definition) doesnt need it, it seems good as-is.

can u explain why i need to put that in there? i wouldnt have thought ud need to put that there.. oh well.

and yes, this is an exercise so i need to make my own rather than use STL or whatever.

Thanks alot though

edit: oh ya almost forgot... yes i am implementing the methods, i just pasted the main outline of the class file, including function prototypes. i excluded the rest because i said i wanted to keep it simple, because i knew the rest was irrelevant

Last edited by nadroj; 02-14-2006 at 05:54 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a debian based distro rmckayfleming Linux - General 24 12-30-2013 04:44 AM
C++ Templated Class and Borland C++ Compiler enemorales Programming 2 09-25-2010 02:30 PM
Difference b/t Kernel stack and User stack hazzyb Linux - Software 2 09-29-2008 07:40 PM
need help making USB based distro tuneout Linux - General 2 08-25-2005 11:24 PM
Making changes to Linux network protocol stack kayko2000 Linux - Networking 0 10-13-2003 05:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration