Student seeking assistance....

S

sandy

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

red floyd

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)...
 
S

sandy

red said:
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.
 
D

David W

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<Job> JC;
....
Job job;
JC.Push(job);

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

DW
 
S

sandy

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<Job> JC;

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

Thomas J. Gritzan

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

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-lite/templates.html#faq-35.12
 
D

David W

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
 
S

sandy

Now you know why I need to take the course!

That helped a lot!

DOH!

Thanks.
<code>
//create a job
Job thisJob;
char menuChoice;
int success = 0;
Stack<Job> JC;

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-lite/templates.html#faq-35.12
 
S

sandy

Now you know why I need to take the course!

That helped a lot!

DOH!

Thanks.
<code>
//create a job
Job thisJob;
char menuChoice;
int success = 0;
Stack<Job> JC;

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-lite/templates.html#faq-35.12
 
P

Puppet_Sock

(e-mail address removed) 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
 
N

Noah Roberts

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top