need help with FIFO QueueItem member definitions

This is a discussion on need help with FIFO QueueItem member definitions within the C++ Programming forums, part of the General Programming Boards category; Code: //QueueItem.h //Declaration of class QueueItem #ifndef QUEUEITEM_H #define QUEUEITEM_H class QueueItem { public: QueueItem(char *pData, int id); // constructor ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    72

    need help with FIFO QueueItem member definitions

    Code:
    //QueueItem.h
    //Declaration of class QueueItem
    
    
    #ifndef QUEUEITEM_H
    #define QUEUEITEM_H
    
    
    
    class QueueItem
    {
    public: 
    	  QueueItem(char *pData, int id);  // constructor
          void setNext(QueueItem *pItem); //set pointer to next Item
          QueueItem* getNext();			//get pointer to next Item
          int getId();					//get Id
          const char* getData();		//get data member
    
    private:
          char mData[30];  // or, use a char* if you want to dynamically alloc memory 
          int mNodeID;
          QueueItem * mpNext;			// pointer to another object of same type
    };//end class QueueItem
    
    #endif
    
    //member function definitions for class QueueItem
    
    #include  <iostream> // allows program to output data
    using namespace std;
    
    #include  <string> 
    
    #include "QueueItem.h" // include definition of class QueueItem from QueueItem.h
    
    QueueItem::QueueItem(char *pData, int id)
    {
    	id = mNodeID;
    	pData = mData;
    }
    void QueueItem::setNext(QueueItem *pItem)
    {
     if ( pItem->mpNext != 0 )
    	 pItem->mpNext
    }
    
    getNext();
    {
    	return mpNext;
    }
    int QueueItem::getId();
    {
    	return mNodeID;
    }
    
    const char* QueueItem::getData()
    {
    	return mData;
    }

    I can't seem to come with member definitions for class QueueItem, can somebody tell me what i'm doing wrong??
    Last edited by jackfraust; 02-21-2009 at 06:55 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why do you have a call to getNext just hanging out in the middle of nowhere? (If that's supposed to be a definition of a function, then you should make it look like all the other definitions of functions.)

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by tabstop View Post
    Why do you have a call to getNext just hanging out in the middle of nowhere? (If that's supposed to be a definition of a function, then you should make it look like all the other definitions of functions.)
    that's my problem, im stuck. I can't come up with the definitions. Im kinda limited when it come to pointers.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you know anything about how a queue works? If so, write it down, and you'll be done. If not, then you should think about how a queue works.

    Apart from the previous "forgot to type the first part" of getNext, and the "forgot to finish typing" setNext, and the "all my assignment statements are backwards", I'm not sure what your worried about.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by tabstop View Post
    Do you know anything about how a queue works? If so, write it down, and you'll be done. If not, then you should think about how a queue works.

    Apart from the previous "forgot to type the first part" of getNext, and the "forgot to finish typing" setNext, and the "all my assignment statements are backwards", I'm not sure what your worried about.
    that's exactly what i'm having problem with (setnext and getnext). But i know that getNext will return *mpNext , so they can't be any issue with that.

    Would setNext b something like this?
    Code:
    void QueueItem::setNext(QueueItem *pItem)
    {
     if ( pItem = NULL )
    	 pItem->mpNext = mData;
    else
         pItem = pItem->mpNext
    
    }
    Last edited by jackfraust; 02-21-2009 at 08:16 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I would hope not. Why would you want to set a pointer-to-QueueItem equal to a pointer-to-char? Why wouldn't you want to set a pointer-to-QueueItem equal to a pointer-to-QueueItem, specifically, the pointer-to-QueueItem that gets passed directly into your function?

    And why would you change some random pointer that isn't related to the object you're dealing with?

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by tabstop View Post
    I would hope not. Why would you want to set a pointer-to-QueueItem equal to a pointer-to-char? Why wouldn't you want to set a pointer-to-QueueItem equal to a pointer-to-QueueItem, specifically, the pointer-to-QueueItem that gets passed directly into your function?

    And why would you change some random pointer that isn't related to the object you're dealing with?
    can you show me an example?

    here is what i can up w/
    Code:
    void QueueItem::setNext(QueueItem *pItem)
    {
     if ( pItem = NULL )
    	 pItem->mpNext = mData;
    else
         pItem = pItem->mpNext
    
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your setNext function must change mpNext. It must not change pItem. It must not change pItem->anything else. You must change the next pointer of the object you are working with.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by tabstop View Post
    Your setNext function must change mpNext. It must not change pItem. It must not change pItem->anything else. You must change the next pointer of the object you are working with.
    so it must be something like this?

    Code:
    void QueueItem::setNext(QueueItem *pItem)
    {
     if ( mpNext = NULL )
    	 pItem->mpNext = mData;
    else
         mpNext = pItem->mpNext
    
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by jackfraust View Post
    so it must be something like this?

    Code:
    void QueueItem::setNext(QueueItem *pItem)
    {
     if ( mpNext = NULL )
    	 pItem->mpNext = mData;
    else
         mpNext = pItem->mpNext
    
    }
    No.

    (1) Sometimes you are changing the parameter passed in, not the object you are working with.
    (2) Sometimes you are changing the correct object, but then you are setting the next item to be something that is quite obviously not what you want it to be. You are told, by the name of the function, that you want to set the next pointer to pItem. Do so.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by tabstop View Post
    No.

    (1) Sometimes you are changing the parameter passed in, not the object you are working with.
    (2) Sometimes you are changing the correct object, but then you are setting the next item to be something that is quite obviously not what you want it to be. You are told, by the name of the function, that you want to set the next pointer to pItem. Do so.
    Code:
    so is it something like this?
    
    void QueueItem::setNext(QueueItem *pItem)
    {
     if ( pItem = NULL)
        pItem = mpNext->pItem;
    
    
    }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Man we've gone all this time and I didn't even notice that your if statement is broken. (It is highly unlikely you want a single = inside an if statement.)

    You must change mpNext. You must not change pItem.

    At this point I would recommend reviewing:
    (1) left vs. right
    (2) the basics of function calls
    (3) what a queue actually is

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by tabstop View Post
    Man we've gone all this time and I didn't even notice that your if statement is broken. (It is highly unlikely you want a single = inside an if statement.)

    You must change mpNext. You must not change pItem.

    At this point I would recommend reviewing:
    (1) left vs. right
    (2) the basics of function calls
    (3) what a queue actually is
    So its something likes this then:
    Code:
    void QueueItem::setNext(QueueItem *pItem)
    {
     if ( pItem == NULL )
    	 pItem == mData->pItem;
    else
        pItem == mpNext->pItem;
    
    }

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    why don't you just tell me how to do it so i can understand it?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm a bad person:

    Code:
    void QueueItem::setNext(QueueItem *pItem)
    {
        mpNext = pItem;
    }
    If you cannot come up with this yourself, when you are explicitly told that you must change mpNext, and that pItem is to be set as the next link, then it's pretty much hopeless anyway.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 09:17 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 12:14 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21