473,387 Members | 1,510 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,387 software developers and data experts.

Student seeking assistance....

I have been given a class, stack to work with. I have created a class
called Job that I want to push on the stack.

I cannot seem to figure out why the push isn't working. (I know the
stack works, I have seen it demonstrated.)

The stack header looks like this:

<code>

#ifndef Stackll_h
#define Stackll_h

#include <stdlib.h>

// Stack: Header File

template <class Etype>
class Stack
{
public:

// Constructor
//
// Input : None
// Purpose: To create an empty Stack
// Output : None

Stack ( );
// Copy constructor
//
// Input : Stack S
// Purpose: To initialize Stack to S
// Output : None

Stack ( const Stack & S );
// Destructor
//
// Input : None
// Purpose: To free memory of Stack
// Output : None

~Stack ( );
// Copy assignment
//
// Input : Stack S
// Purpose: To assign S to current Stack
// Output : Current Stack

const Stack & operator= ( const Stack & S );
// Push
//
// Input : Element E
// Purpose: To place E on the top of Stack
// Output : 1 if successful; 0 otherwise

int Push ( const Etype & E );
// Pop
//
// Input : None
// Purpose: To delete the top element of Stack
// Output : 1 if successful; 0 otherwise

int Pop ( );
// Retrieve
//
// Input : None
// Purpose: To return the top element E of Stack
// Output : Element E and 1 if successful; 0 otherwise

int Retrieve ( Etype & E ) const;
// Empty
//
// Input : None
// Purpose: To check if Stack is empty
// Output : 1 if empty; 0 otherwise

int Empty ( ) const;
// Clear
//
// Input : None
// Purpose: To re-initialize Stack to empty
// Output : None

void Clear ( );
private:

// Data members

// A singly-linked Stack node
struct StackNode
{
Etype Element;
StackNode *Next;

// StackNode constructors
StackNode ( ) : Next ( NULL )
{ }
StackNode ( const Etype & E, StackNode *P = NULL ) :
Element ( E ), Next ( P )
{ }
};

// Pointer to the top of Stack
StackNode *Top;
};

#endif
</code>

My main where I am trying to Push onto the stack looks like this (part
of it anyway...)

<code>
//create a job
Job *thisJob;
char menuChoice;
int success = 0;

do
{
system("cls");
Stack<Job*JC;
mainMenu();

cin >menuChoice;
menuChoice = toupper(menuChoice);
cin.ignore(1, '\n');

switch(menuChoice)
{
case 'N':
//assign values
thisJob = new Job;
assignJobValues(thisJob);
success = JC->Push(&thisJob);

</code>

The error I get is: no matching function for call to
`Stack<Job>::Push(Job**)'

Thanks for your time!

Oct 5 '06 #1
10 1577
sa***@murdocks.on.ca wrote:
I have been given a class, stack to work with. I have created a class
called Job that I want to push on the stack.

I cannot seem to figure out why the push isn't working. (I know the
stack works, I have seen it demonstrated.)

The stack header looks like this:

<code>

#ifndef Stackll_h
#define Stackll_h

#include <stdlib.h>

// Stack: Header File

template <class Etype>
class Stack
{
public:

[redacted]

// Push
//
// Input : Element E
// Purpose: To place E on the top of Stack
// Output : 1 if successful; 0 otherwise

int Push ( const Etype & E );

[redacted]
};

#endif
</code>

My main where I am trying to Push onto the stack looks like this (part
of it anyway...)

<code>
//create a job
Job *thisJob;
char menuChoice;
int success = 0;

do
{
system("cls");
Stack<Job*JC;
mainMenu();

cin >menuChoice;
menuChoice = toupper(menuChoice);
cin.ignore(1, '\n');

switch(menuChoice)
{
case 'N':
//assign values
thisJob = new Job;
assignJobValues(thisJob);
success = JC->Push(&thisJob);

</code>

The error I get is: no matching function for call to
`Stack<Job>::Push(Job**)'
Look at the template, and look at what you're pushing.

Stack::Push is expecting a Job. And you're passing it the address of a
pointer to a Job (a Job**).

Why are you dynamically allocating your stack? Is this a remnant of old
Java programming, where everything had to be "new'ed"? Ditto for your
job that you're creating and pushing (well, trying to push anyways)...
Oct 6 '06 #2

red floyd wrote:
>
Look at the template, and look at what you're pushing.

Stack::Push is expecting a Job. And you're passing it the address of a
pointer to a Job (a Job**).

Why are you dynamically allocating your stack? Is this a remnant of old
Java programming, where everything had to be "new'ed"? Ditto for your
job that you're creating and pushing (well, trying to push anyways)...
Well, I don't have much control over the stack, I am supposed to just
use it. If I am not using it correctly I would love to know how I
should be doing it.

As for the Job, I can use the 'new' operator and get a new address
space. If I don't use 'new' I don't know how to use the same variable
over and over without using the same memory space over and over.

Do you know what I mean?

Each time I loop I need to create a new job which I then add values to
(it's properties) and then send it off to the stack.

So I declare my Job using a pointer. If I knew how to tell the system
to 'pass the thing this pointer points at' I think I could call the
function.

Otherwise I need some way to create a new job each time through the
loop.

And feeling rather like a lost student, I don't know how.

Oct 6 '06 #3
<sa***@murdocks.on.cawrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
I have been given a class, stack to work with. I have created a class
called Job that I want to push on the stack.

I cannot seem to figure out why the push isn't working. (I know the
stack works, I have seen it demonstrated.)

The stack header looks like this:
[snip]
int Push ( const Etype & E );
do
{
system("cls");
Stack<Job*JC;
What does JC point to?
mainMenu();

cin >menuChoice;
menuChoice = toupper(menuChoice);
cin.ignore(1, '\n');

switch(menuChoice)
{
case 'N':
//assign values
thisJob = new Job;
Any particular reason for using 'new' to create the Job?
assignJobValues(thisJob);
success = JC->Push(&thisJob);
Here you want *thisJob, not &thisJob. Actually, &thisJob is better because it won't compile. If you
fix that and run it the program will very likely crash because JC has not been initialized.

Do you delete thisJob when you are finished with it?
>
</code>

The error I get is: no matching function for call to
`Stack<Job>::Push(Job**)'
Right. &thisJob takes the address of thisJob to produce a Job**.

I suggest that you make things much easier for yourself and stop using pointers except where you
really need them. You could have had:

Stack<JobJC;
....
Job job;
JC.Push(job);

Looks cleaner and simpler, and there are no pointers, so nothing to delete later.

DW
Oct 6 '06 #4

I have made some changes to try to implement what was suggested, I have
left the stack alone, but this is now my code from main()
<code>
//create a job
Job thisJob;
char menuChoice;
int success = 0;
Stack<JobJC;

do
{
system("cls");
mainMenu();

cin >menuChoice;
menuChoice = toupper(menuChoice);
cin.ignore(1, '\n');

switch(menuChoice)
{
case 'N':
//assign values
//thisJob = new Job;
//cout << thisJob;
assignJobValues(&thisJob);
success = JC.Push(thisJob);
</code>

No pointer for the stack, and I am not creating a new memory space for
the job. (I think I will end up with only one job no matter how many
times I set the properties, but I figure what the heck. Try it and
see).

When I try to compilee now I still get errors on that last line:
[Linker error] undefined reference to `Stack<Job>::Stack()'
[Linker error] undefined reference to `Stack<Job>::Push(Job const&)'
[Linker error] undefined reference to `Stack<Job>::~Stack()'
[Linker error] undefined reference to `Stack<Job>::~Stack()'

So I am really no closer... and even more confused.

Oct 6 '06 #5
sa***@murdocks.on.ca schrieb:
<code>
//create a job
Job thisJob;
char menuChoice;
int success = 0;
Stack<JobJC;

do
{
system("cls");
mainMenu();

cin >menuChoice;
menuChoice = toupper(menuChoice);
cin.ignore(1, '\n');

switch(menuChoice)
{
case 'N':
//assign values
//thisJob = new Job;
//cout << thisJob;
assignJobValues(&thisJob);
success = JC.Push(thisJob);
</code>

No pointer for the stack, and I am not creating a new memory space for
the job. (I think I will end up with only one job no matter how many
times I set the properties, but I figure what the heck. Try it and
see).
The Stack stores copies of the variables. So every Push will copy the
object into a new location.
For how to use the Stack, you should ask the programmer of this class.
When I try to compilee now I still get errors on that last line:
[Linker error] undefined reference to `Stack<Job>::Stack()'
[Linker error] undefined reference to `Stack<Job>::Push(Job const&)'
[Linker error] undefined reference to `Stack<Job>::~Stack()'
[Linker error] undefined reference to `Stack<Job>::~Stack()'

So I am really no closer... and even more confused.
You have to include the implementation file for the Stack template as well.
The header alone is not enough. Read the FAQ, 35.12 and 35.13:
http://www.parashift.com/c++-faq-lit...html#faq-35.12

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 6 '06 #6
<sa***@murdocks.on.cawrote in message news:11*********************@m7g2000cwm.googlegrou ps.com...
>
red floyd wrote:

Look at the template, and look at what you're pushing.

Stack::Push is expecting a Job. And you're passing it the address of a
pointer to a Job (a Job**).

Why are you dynamically allocating your stack? Is this a remnant of old
Java programming, where everything had to be "new'ed"? Ditto for your
job that you're creating and pushing (well, trying to push anyways)...

Well, I don't have much control over the stack, I am supposed to just
use it. If I am not using it correctly I would love to know how I
should be doing it.

As for the Job, I can use the 'new' operator and get a new address
space. If I don't use 'new' I don't know how to use the same variable
over and over without using the same memory space over and over.

Do you know what I mean?
I think so, but it's not relevant to your code. When you push an object onto the stack, the stack
stores a _copy_ of it. It's not the original object that the stack keeps, so you once you've pushed
it you can safely get rid of it. Note the StackNode member:
Etype Element;

That's a whole other object. It has to be because it is not pointing to or referring to the object
you pushed. Therefore there is no problem in "using the same memory space over and over" for the
objects you are pushing.
Each time I loop I need to create a new job which I then add values to
(it's properties) and then send it off to the stack.

So I declare my Job using a pointer. If I knew how to tell the system
to 'pass the thing this pointer points at' I think I could call the
function.

Otherwise I need some way to create a new job each time through the
loop.
Sufficient would be: Job job;
And feeling rather like a lost student, I don't know how.
It should become clearer once you understand what is actually stored in what region of memory.

DW
Oct 6 '06 #7
Now you know why I need to take the course!

That helped a lot!

DOH!

Thanks.

Thomas J. Gritzan wrote:
sa***@murdocks.on.ca schrieb:
<code>
//create a job
Job thisJob;
char menuChoice;
int success = 0;
Stack<JobJC;

do
{
system("cls");
mainMenu();

cin >menuChoice;
menuChoice = toupper(menuChoice);
cin.ignore(1, '\n');

switch(menuChoice)
{
case 'N':
//assign values
//thisJob = new Job;
//cout << thisJob;
assignJobValues(&thisJob);
success = JC.Push(thisJob);
</code>

No pointer for the stack, and I am not creating a new memory space for
the job. (I think I will end up with only one job no matter how many
times I set the properties, but I figure what the heck. Try it and
see).

The Stack stores copies of the variables. So every Push will copy the
object into a new location.
For how to use the Stack, you should ask the programmer of this class.
When I try to compilee now I still get errors on that last line:
[Linker error] undefined reference to `Stack<Job>::Stack()'
[Linker error] undefined reference to `Stack<Job>::Push(Job const&)'
[Linker error] undefined reference to `Stack<Job>::~Stack()'
[Linker error] undefined reference to `Stack<Job>::~Stack()'

So I am really no closer... and even more confused.

You have to include the implementation file for the Stack template as well.
The header alone is not enough. Read the FAQ, 35.12 and 35.13:
http://www.parashift.com/c++-faq-lit...html#faq-35.12

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 6 '06 #8
Now you know why I need to take the course!

That helped a lot!

DOH!

Thanks.

Thomas J. Gritzan wrote:
sa***@murdocks.on.ca schrieb:
<code>
//create a job
Job thisJob;
char menuChoice;
int success = 0;
Stack<JobJC;

do
{
system("cls");
mainMenu();

cin >menuChoice;
menuChoice = toupper(menuChoice);
cin.ignore(1, '\n');

switch(menuChoice)
{
case 'N':
//assign values
//thisJob = new Job;
//cout << thisJob;
assignJobValues(&thisJob);
success = JC.Push(thisJob);
</code>

No pointer for the stack, and I am not creating a new memory space for
the job. (I think I will end up with only one job no matter how many
times I set the properties, but I figure what the heck. Try it and
see).

The Stack stores copies of the variables. So every Push will copy the
object into a new location.
For how to use the Stack, you should ask the programmer of this class.
When I try to compilee now I still get errors on that last line:
[Linker error] undefined reference to `Stack<Job>::Stack()'
[Linker error] undefined reference to `Stack<Job>::Push(Job const&)'
[Linker error] undefined reference to `Stack<Job>::~Stack()'
[Linker error] undefined reference to `Stack<Job>::~Stack()'

So I am really no closer... and even more confused.

You have to include the implementation file for the Stack template as well.
The header alone is not enough. Read the FAQ, 35.12 and 35.13:
http://www.parashift.com/c++-faq-lit...html#faq-35.12

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 6 '06 #9
sa***@murdocks.on.ca wrote:
[newbie difficulty with C++]

You need to get and read a good C++ text. A reasonable one
that gets lots of "thumbs up" around here is _Accelerated C++_
by Koenig and Moo.
Socks

Oct 6 '06 #10

sa***@murdocks.on.ca wrote:
My main where I am trying to Push onto the stack looks like this (part
of it anyway...)

<code>
//create a job
Job *thisJob;
char menuChoice;
int success = 0;

do
{
system("cls");
Stack<Job*JC;
At least two problems here. First, JC is not initialized. However,
even if it where this would not work. You are creating a new one every
iteration through the loop. That is almost certainly not what you
want. And I don't know why your job is outside the loop, it IS wanted
to be created every iteration.

Oct 6 '06 #11

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

Similar topics

6
by: James Turner | last post by:
I am trying to store formatted text (windows format) into a MySQL database and then retrieve it. The field in the database is a varchar. I cut and paste the test into a form field formatted, then...
1
by: rdshultz | last post by:
I'm a complete newbie. Need to insert a Company logo into a database column to use later on in a check printing application. Read how to insert the pointer instead of the object into the column. ...
2
by: Anderson | last post by:
Does anyone have a sample student record system in access. Or would you point me somewhere where I can get examples. many thanks. Andy
0
by: michael horthum | last post by:
- i am not a beginner, books, or online tutorials don't help, please save your advises- i want to learn c# from someone who knows it well. concepts and fundamentals i currently have (gdi+,...
1
by: BinnuChowdary | last post by:
Very Good news for all freshers and especially those who want to shift to Dotnet Technologies We have established an Training and Development Center Named Z-Axis Technologies in Our City at KPHB,...
1
by: GS | last post by:
I am looking for some software package for students information for school, is there any open-source package available?. This should collect complete student information like: student details,...
5
by: sandy | last post by:
I am a student with an assignement due tomorrow. I am to write a sort of a memory manager, I don't want to go into all of the details (it's long). I have the following header file for my memory...
6
by: sandy | last post by:
I am working on an assignment which is a 'discrete event simulation'. I have a class called event, I need to add it to a priority queue and I cannot for some reason. I am sure it's the syntax... ...
0
by: Racqetsports | last post by:
Hi there, In a gradebook database, student grades must be computed from 2 scores: a Daily grade, and then scores from Assignments. Knowing about nested forms, I am requesting direction on how to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
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...

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.