Question:
C++ Class/Queue problem?
FROSTYSMOOTH
2010-09-27 22:11:03 UTC
So this is my first attempt at creating a class in C++ so please forgive me if it is kind of scattered. The original program worked by doing it with functions, but I am trying to create a class(for the first time) that does the same thing as the original. I have 3 files in which I am working with along with the queue.txt file that I am reading the data from. I am generating several errors all related to what seem to be the same thing. error C3867: 'QueueMgr::BackQ': function call missing argument list; use '&QueueMgr::BackQ' to create a pointer to member. I am aware that some of my functionality isn't in yet but I originally generated 30 errors at first then got it down to 6 which is where I am at now. I think combination of lack of sleep and staring at the computer screen have me missing some aspects. I also commented out some parts that were generating errors.
//File: QueueMgr.h

#ifndef _QUEUEMGR_H
#define _QUEUEMGR_H
//#includes

using namespace std;


class QueueMgr
{
private:
queue qlist;//make the queue object
ifstream input;//for reading file
string filename;
string description;//for title of the queue
bool bFileReady;//set when a file is open and still has lines to read
void OpenFile();//opens the file, assigns bFileReady

public:
QueueMgr();//default constructror, does not open a file
QueueMgr(string fn);//overloaded constructor, passes in the file name
~QueueMgr();//destructor closes the file
void OpenFile(string fn);//calls OpenFile()
bool isReady(){return bFileReady;}

void AddToQ();//adds to the queue from file, and shows
void ServeQ();//serves out the first item in the queue
void ShowQ();//returns size of queue
void FrontQ();//shows item in the front of the queue
void BackQ();//shows item in the back of the queue
void ClearQ();//clears the queue
string GetDescription(){return description;}
};

#endif
Four answers:
husoski
2010-09-27 23:10:45 UTC
Provided that and are included, the class definition compiles fine.



Some things that I noticed in a short browse of the code:



1. You need QueueManager:: in front of the methods when you define them. Unless you have static methods, this is the *only* time you will put QueueManager:: in front of a method name. For example:



void QueueManager::OpenFile(string fn)

{

// body goes here

}



2. You need to implement any constructors actually used, plus the destructor, even if the implemenation is empty. Example:



QueueManager::QueueManager() // Default constructor

: queue(), ifstream(), filename(""), description(""), bFileReady(false)

{

}



QueuManager::~QueueManager()

{ if (filename.length() != 0) ifsteam.close(); }



3. You need to create an instance of the QueueManager class, and call the methods on that object. This can be done by including something like:



QueueManager myqueue; // or myqueue(FILE_IN) maybe...



Then use myqueue.ShowQ() to call the ShowQ method on that object. You only use QueueManager::methodname() to call a method if that method is declared static.



4. The implementation of AddToQueue() makes qlist a parameter. Delete that. qlist is a member variable in the object...you don't need to pass it.



void QueueManager::AddToQ()

{ /* body */ }



5. In OpenFile(), you have a file name argument, but use the constant FILE_IN instead. you should use the fn argument instead, and also copy that string to the filename member variable.



6. Finally (but not the last problem, I'll wager), you need a loop in main() around the prompt and switch statement, so that you can exercise more than one method call when the program is run.
Cubbi
2010-09-28 06:00:03 UTC
You got a few remarks already, of varying quality, but let me mention an important design concept that, I think, beginners should be aware of, to steer in the right direction: C++'s power, uniqueness, and indispensability among all the competing modern programming languages comes from the concept called RAII (resource allocation is initialization), or, more precisely, SBRM (scope-bound resource management). A properly written C++ class acquires all resources and becomes completely ready and functional the moment its constructor finishes execution. It never needs a separate init function or an "isReady()" predicate. If it constructed, it *is* ready. Likewise, the proper C++ class's destructor is the only point where any resources are released and the only way to do so.



What it means, in your case, is that unless you have an actual USE of an object of type QueueMgr which has no file opened, or has a file opened, but not read from, your file must be opened in the constructor and the reading of the queue from the file (your AddToQ()) should also be done in the constructor. Nobody needs a default constructor that does nothing. Although since you have a default file name, you could actually make a default constructor that attempts to open and read queue from the default file. You don't need a destructor at all because both std::queue and std::ifstream are SBRM classes themselves, and will properly clear memory and close open files when destroyed.
Little Princess
2010-09-27 23:06:08 UTC
You've got several things wrong. In your .cpp file, you need to change

void AddToQ(queue qlist)

to

void QueueMgr::AddToQ(queue qlist)

so that the compile knows AddToQ belongs to the QueueMgr class. You'll need to do this for all the routines that belong to that class.



Next, you need to create at least one instance of the class. Have something like

QueueMgr myQueueManager; /*or*/ QueueMgr myQueueManager( "myFilename.dat" );

It will call the appropriate constructor when you declare the instance.

Then when you want to call the various routines do it like

myQueueManager.ShowQ(qlist);

You can have multiple instances of your queue manager simply by declaring more instances of it. And you can call ShowQ for different ones and it will use each one's individual set of variables.



As you've posted it, you don't have another } to end either the switch, the while loop, nor the main.



I'm sure you've got several more problems in there, but this will at least get you closer.
2016-04-20 21:24:58 UTC
When you overload the "Insertion" and "Extraction" operators ( >> , << ), the function must return istream& and ostream& (ALL respectively) see sources:


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...