Thread: Homework Help - Create FIFO Queue class

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    27

    Homework Help - Create FIFO Queue class

    The assignment says to "Create a Queue class that implements a queue abstraction."

    Naturally, I'm running into errors:
    main_Hardesty.cpp:30:25: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
    QueueItem_Hardesty.cpp: In constructor ‘QueueItem::QueueItem(char*, int)’:
    QueueItem_Hardesty.cpp:31:47: error: incompatible types in assignment of ‘char*’ to ‘char [30]’
    QueueItem_Hardesty.cpp:33:21: error: ‘pDate’ was not declared in this scope

    I'm copying my elements incorrectly, but I'm not knowledgeable enough to fix it. Can you assist? Thank you.

    Here's the relevant stuff from my cpp file
    Code:
    #include <iostream>         //my standard cout/cin stuff
    #include <cstdio>           //some C stuff for good measure
    #include <iomanip>          //the setfill() and setw() stuff
    #include <string>           //self-explanatory
    #include "QueueItem_Hardesty.h"  /*include my QueueItem_Hardesty header 
                                      file with class QueueItem*/
    #include "Queue_Hardesty.h"      /*include my Queue_Hardesty.h file with 
                                       class Queue*/
    using std::cout;
    using std::cin;
    using std::endl;
    using std::setfill;
    using std::setw;
    using std::string;
    using std::copy;
    
    //***LINE 29*** my QueueItem constructor
    QueueItem::QueueItem(char *pData, int id)
       : _data (pData), _itemID (id), _pNext (NULL)  //private variables now global variables
    {
       std::copy(_data, *pDate + 30, std::begin(pDate));
    }  //end constructor/function
    And my header file

    Code:
    //***LINE 13*** Prevent multiple inclusions of header
    #ifndef QueueItem_H
    #define QueueItem_H
    
    //this will set/get data members, not manipulate the linked lists
    class QueueItem
    {
    public: 
       QueueItem(char *pData, int id);        //ctor
       void setNext(QueueItem *pItem);        //set pointer to next Item
       QueueItem* getNext() const;            //get pointer for next Item
       int getId() const;                     //get Id
       const char* getData() const;
    
    private:
       char _data[30];              //data value character string/array
       const int _itemID;           //unique ID for item in queue
       QueueItem* _pNext;           //next item in queue
    }; //end class QueueItem
    
    #endif

    Crap, you need my main function.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    using std::cout;
    using std::endl;
    using std::setfill;
    using std::setw;
    using std::string;
    
    #include "Queue_Hardesty.h"
    
    int main()
    {
       //***LINE 25*** construct a queue called myQueue
       Queue myQueue;
       cout << "Creating a queue.  Program is now started." << endl;
       myQueue.removeItem();      //call Remove, should do nothing since Queue is empty
    
       myQueue.addItem("red");    //
       myQueue.addItem("green");
       myQueue.addItem("blue");
       myQueue.addItem("orange");
       myQueue.print();           //prints contesnts of queue (ID & data)
       myQueue.removeItem();
       myQueue.removeItem();
    
    /*...
      etc, etc, etc
      ...*/
    
    } //end function main

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you trying to use C-strings instead of std::strings? You seem to know about this class, since you included the <string> header.


    Jim

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    I'm sorry, can you be a little more specific? Do you mean making "char _date[30]" a string instead of a character array? The instructor gave us a few example or starter codes in the assignment write-up, so maybe that's why I'm using them, but if I can't make 'em work, eff em, I'm open to using other code.
    Last edited by boygenuis; 06-10-2014 at 06:57 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by boygenuis
    Do you mean making "char _date[30]" a string instead of a character array?
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by boygenuis View Post
    I'm sorry, can you be a little more specific? Do you mean making "char _date[30]" a string instead of a character array? The instructor gave us a few example or starter codes in the assignment write-up, so maybe that's why I'm using them, but if I can't make 'em work, eff em, I'm open to using other code.
    You will see that if you use std::string, then everything just "works."
    No need to worry about the length of the array.
    You can assign strings just like variables (e.g. operator =).
    No need to worry about fitting a string into a small space.
    And much, much more.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    Whelp, I clearly don't get what I'm supposed to do because I switched my QueueItem.h to read "std::string _data;" instead of "char _data[30];" but I'm still getting the same errors as before. What did I miss from the instructions?

    Errors:
    Code:
    main_Hardesty.cpp: In function ‘int main()’:
    main_Hardesty.cpp:30:25: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
    (THE ABOVE ERROR OCCURS FOR EVERY TIME I CALL myQueue.addItem IN main)
    QueueItem_Hardesty.cpp: In member function ‘const char* QueueItem::getData() const’:
    QueueItem_Hardesty.cpp:53:11: error: cannot convert ‘const string {aka const std::basic_string<char>}’ to ‘const char*’ in return
    Here's the amended QueueItem.h

    Code:
    //***LINE 13*** Prevent multiple inclusions of header
    #ifndef QueueItem_H
    #define QueueItem_H
    
    //this will set/get data members, not manipulate the linked lists
    class QueueItem
    {
    public: 
       QueueItem(char *pData, int id);        //ctor
       void setNext(QueueItem *pItem);        //set pointer to next Item
       QueueItem* getNext() const;            //get pointer for next Item
       int getId() const;                     //get Id
       const char* getData() const;
    
    private:
       std::string _data;           //data value character string/array
       const int _itemID;           //unique ID for item in queue
       QueueItem* _pNext;           //next item in queue
    }; //end class QueueItem
    
    #endif
    And my QueueItem.cpp

    Code:
    #include <iostream>         //my standard cout/cin stuff
    #include <cstdio>           //some C stuff for good measure
    #include <iomanip>          //the setfill() and setw() stuff
    #include <string>           //self-explanatory
    #include "QueueItem_Hardesty.h"  /*include my QueueItem_Hardesty header 
                                      file with class QueueItem*/
    #include "Queue_Hardesty.h"      /*include my Queue_Hardesty.h file with 
                                       class Queue*/
    using std::cout;
    using std::cin;
    using std::endl;
    using std::setfill;
    using std::setw;
    using std::string;
    using std::copy;
    
    //my QueueItem constructor
    QueueItem::QueueItem(char *pData, int id)
       : _data (pData), _itemID (id), _pNext (NULL)  //private variables now global variables
    {
       //std::copy(_data, pData + 30, std::begin(pData));
    }  //end constructor/function
    
    ...
    
    const char* QueueItem::getData() const
    {
       return _data;
    }  //***LINE 54*** end getData function
    If I haven't said it before, I appreciate your help as I crawl through this.

    Aaaannd, lest I forget my main.cpp

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    using std::cout;
    using std::endl;
    using std::setfill;
    using std::setw;
    using std::string;
    
    #include "Queue_Hardesty.h"
    
    int main()
    {
       //construct a queue called myQueue
       Queue myQueue;
       cout << "Creating a queue.  Program is now started." << endl;
       myQueue.removeItem();      //call Remove, should do nothing since Queue is empty
    
       myQueue.addItem("red");    //
       myQueue.addItem("green");
       myQueue.addItem("blue");
       myQueue.addItem("orange");
       myQueue.print();           //prints contesnts of queue (ID & data)
       myQueue.removeItem();
       myQueue.removeItem();
       myQueue.addItem("black");
       myQueue.addItem("brown");
       myQueue.addItem("gray");
       myQueue.addItem("purple");
       myQueue.print();
       myQueue.removeItem();
       myQueue.removeItem();
       myQueue.removeItem();
       myQueue.removeItem();
       myQueue.print();
       myQueue.eraseQueue();
       myQueue.addItem("white");
       myQueue.addItem("tan");
       myQueue.addItem("pink");
       myQueue.print();
       myQueue.eraseQueue();
       myQueue.print();
    
    } //end function main

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why the char *? Shouldn't tese be strings not char *?

    Code:
    ...
       const char* getData() const;
    ...
    QueueItem::QueueItem(char *pData, int id)
    (THE ABOVE ERROR OCCURS FOR EVERY TIME I CALL myQueue.addItem IN main)
    So where is addItem()?

    Jim

  8. #8
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    So, if I change every instance of "char *pData" to "std::string pData", will this break my linked list?

    Here's the addItem() code given to us by the instructor.
    Code:
    //dynamically create and init a new QueueItem object
    void Queue::addItem(char *pData)
    {
       QueueItem *pItem = new QueueItem(pData, ++_itemCounter);
    
       if (pHead == 0)   //check for an empty queue
          pHead = pTail = pItem;
       else               //link new item onto end of list using _pTail pointer
       {
          pTail->setNext(pItem);
          pTail = pItem;
       } 
    }  //end addItem function
    Your thoughts?

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    If you examine the documentation, you can indeed put a constant char pointer into the std::string constructor so it is valid to construct std::string with const char* or char* which will automatically be interpreted as constant.

    I'm not sure what your structure actually looks like (I'm just skimming the topic) but your nodes should look like this :
    Code:
    struct queue_node {
    
          std::string data;
          queue_node *next;
    };
    You can easily initialize data with a char*.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by boygenuis View Post
    Whelp, I clearly don't get what I'm supposed to do because I switched my QueueItem.h to read "std::string _data;" instead of "char _data[30];" but I'm still getting the same errors as before. What did I miss from the instructions?
    GetData shall return a std::string
    QueueItem shall take a std::string.
    And lastly, your node shall contain a std::string.
    That is, switch out all instances of char*/const char* for std::string.

    Quote Originally Posted by boygenuis View Post
    So, if I change every instance of "char *pData" to "std::string pData", will this break my linked list?

    Your thoughts?
    No, it will not break your list.
    You know variables by know. You know that you can make a copy of a variable's contents. You know you can assign a value to a variable. You know that if you make a copy of a variable and let the original variable die, then the copy still retains its value. This is the variable semantics we've come to know and love. std::string works exactly the same way as variables. There are no special exceptions. Make a copy of it in the node. Fine. Still no problem. Assign it to to another variable. Fine. Still no problem.

    Quote Originally Posted by MutantJohn View Post
    If you examine the documentation, you can indeed put a constant char pointer into the std::string constructor so it is valid to construct std::string with const char* or char* which will automatically be interpreted as constant.

    I'm not sure what your structure actually looks like (I'm just skimming the topic) but your nodes should look like this :

    You can easily initialize data with a char*.
    Or you can... you know, just use std::string to begin with.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2014
    Posts
    27
    Thank you all very much for your patience and hand-holding. You've earned your mitzvah wings today!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Help - Create C++ Date class
    By boygenuis in forum C++ Programming
    Replies: 22
    Last Post: 06-03-2014, 02:12 PM
  2. Priority queue and FIFO
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 02-29-2012, 02:32 PM
  3. Help with FIFO queue
    By Martin_T in forum C Programming
    Replies: 10
    Last Post: 11-08-2009, 10:30 AM
  4. understanding queue FIFO
    By arastoo.s in forum C Programming
    Replies: 6
    Last Post: 08-18-2009, 10:16 AM
  5. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM

Tags for this Thread