473,396 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using Classes

Hi -- me again. This time, I swear I have an on-topic post. :)
Again I apologise for any etiquette violations -- I don't use Usenet
frequently, so I'm not terribly aware of the minute rules. Bear with me
if I goof again.

Anyway, I have a program with two files, a header and a cpp main. The
header contains a class that handles a stack of ints (don't mind that I
simplified it down to a few lines for ease of reading). This code builds
fine.

// MAIN FILE

#include <iostream>
#include "stack2.h"

using namespace std;

int main () {
stack myStack;
return 0;
}

// -----------
// HEADER FILE

// FILE: stack2.h

#ifndef MAIN_SAVITCH_STACK2_H
#define MAIN_SAVITCH_STACK2_H

#include <cstdlib> // Provides NULL and size_t
#include "node2.h"

class stack {
public:
stack( ) { top_ptr = NULL; }
void push(const int& entry);
private:
int *top_ptr; // Points to top of stack
} };

#endif

// END CODE

However, I want to turn the above class into a template class. Tacking
on template <class Item> to the top of the class and changing int *top_
ptr to Item *top_ptr seems like it would be all that's necessary, but it
doesn't compile when I do this. The compiler complains that a) parse
error before ';', b) 'stack' is undeclared.

What is wrong? Thanks.

Jul 19 '05 #1
2 1476
I realised I forgot a few things:
* I didn't just simplify the code for reading here...I commented out all
the excess stuff just to accomplish this basic task.
* I realise push()'s parameter would have to be converted to the Item
template as well as the other two changes I thought of. Still, because I
never use push(), it shouldn't impact the compiling.

Thanks.

In <20********************@news.cwru.edu> Will Oram wrote:
Hi -- me again. This time, I swear I have an on-topic post. :)
Again I apologise for any etiquette violations -- I don't use Usenet
frequently, so I'm not terribly aware of the minute rules. Bear with
me if I goof again.

Anyway, I have a program with two files, a header and a cpp main. The
header contains a class that handles a stack of ints (don't mind that
I simplified it down to a few lines for ease of reading). This code
builds fine.

// MAIN FILE

#include <iostream>
#include "stack2.h"

using namespace std;

int main () {
stack myStack;
return 0;
}

// -----------
// HEADER FILE

// FILE: stack2.h

#ifndef MAIN_SAVITCH_STACK2_H
#define MAIN_SAVITCH_STACK2_H

#include <cstdlib> // Provides NULL and size_t
#include "node2.h"

class stack {
public:
stack( ) { top_ptr = NULL; }
void push(const int& entry);
private:
int *top_ptr; // Points to top of stack
} };

#endif

// END CODE

However, I want to turn the above class into a template class. Tacking
on template <class Item> to the top of the class and changing int *top_
ptr to Item *top_ptr seems like it would be all that's necessary, but
it doesn't compile when I do this. The compiler complains that a)
parse error before ';', b) 'stack' is undeclared.

What is wrong? Thanks.

Jul 19 '05 #2
Will Oram wrote in news:20********************@news.cwru.edu:
However, I want to turn the above class into a template class. Tacking
on template <class Item> to the top of the class and changing int
*top_ ptr to Item *top_ptr seems like it would be all that's
necessary, but it doesn't compile when I do this. The compiler
complains that a) parse error before ';', b) 'stack' is undeclared.

What is wrong? Thanks.


Note the "stack< int >" in main(), also the argument type of push().

template < typename Item >
class stack
{
public:
stack( ) { top_ptr = NULL; }
void push(const Item& entry);
private:
Item *top_ptr; // Points to top of stack
};
int main()
{
stack< int > myStack;
}

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: bissatch | last post by:
Hi, Generally if I re-use code, I use a function. If I need to use these functions over a number of pages I write the function to an include file where all pages have access. So when should I...
6
by: Jake D | last post by:
I understand how to load a DLL in C++ and use functions from it, however I have a DLL which I did not create which has some classes in it. I can't seem to find out how to use the classes to make...
1
by: Donald Welker | last post by:
I'm building a web forms project and would like to provide consistent connection, data adapter, dataset objects, etc. across multiple ASPX pages. I'm having problems with trying to make these...
0
by: Stu | last post by:
Hi, Ive been using classes as output from webservices which until now have worked well and have serialised into the xml i wanted. Now i want to have a structure of clients which have accounts...
0
by: The Confessor | last post by:
I have a rather sprawling application-in-progress in Visual Basic .NET 2003, and I'm using very specific naming conventions to avoid confusion and make the function of my code as self-evident as...
5
by: Richard Gromstein | last post by:
Hello, I have an exercise that I need to finish very soon and I really need help understanding what to do and how exactly to do it. I am working on reading the chapter right now and working on it...
3
by: apedosmil8 | last post by:
I am having problems with using classes. I'm not sure why this program will not run. I have pointed out the problem. Thanks #include <iostream> #include <iomanip> #include <cmath> using...
2
by: =?Utf-8?B?d2lubGlu?= | last post by:
Hello 1) I agree that useing classes is the best way to use VB.net. The question is how do you write a VB program without using classes? 2) For my own edification where can I see an actual...
3
by: Ramu528 | last post by:
Hi friends, I am populating a drop box from a table jos_qualifications. can anybody tell me how to populate a drop box using classes. something like $db->setQuery( $query ); this is my code...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.