Student seeking assistance....

Discussion in 'C++' started by sandy@murdocks.on.ca, Oct 6, 2006.

  1. Guest

    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 6, 2006
    #1
    1. Advertising

  2. red floyd Guest

    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)...
     
    red floyd, Oct 6, 2006
    #2
    1. Advertising

  3. Guest

    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, 2006
    #3
  4. David W Guest

    <> wrote in message
    news:...
    > 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
     
    David W, Oct 6, 2006
    #4
  5. Guest

    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.
     
    , Oct 6, 2006
    #5
  6. schrieb:
    > <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

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
     
    Thomas J. Gritzan, Oct 6, 2006
    #6
  7. David W Guest

    <> wrote in message news:...
    >
    > 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
     
    David W, Oct 6, 2006
    #7
  8. Guest

    Now you know why I need to take the course!

    That helped a lot!

    DOH!

    Thanks.

    Thomas J. Gritzan wrote:
    > schrieb:
    > > <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
    >
    > --
    > Thomas
    > http://www.netmeister.org/news/learn2quote.html
     
    , Oct 6, 2006
    #8
  9. Guest

    Now you know why I need to take the course!

    That helped a lot!

    DOH!

    Thanks.

    Thomas J. Gritzan wrote:
    > schrieb:
    > > <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
    >
    > --
    > Thomas
    > http://www.netmeister.org/news/learn2quote.html
     
    , Oct 6, 2006
    #9
  10. Puppet_Sock Guest

    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
     
    Puppet_Sock, Oct 6, 2006
    #10
  11. Noah Roberts Guest

    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.
     
    Noah Roberts, Oct 6, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chen Bin

    A student's question

    Chen Bin, Aug 31, 2003, in forum: VHDL
    Replies:
    7
    Views:
    535
  2. Jeff Goslin

    Newbie seeking VB to ANSI C Conversion assistance

    Jeff Goslin, Nov 5, 2003, in forum: C Programming
    Replies:
    14
    Views:
    728
    Jimmy
    Nov 23, 2003
  3. Replies:
    12
    Views:
    448
    Dennis Lee Bieber
    Nov 14, 2006
  4. Replies:
    5
    Views:
    328
  5. Replies:
    6
    Views:
    356
Loading...

Share This Page