So my original program ran and did what it needed to do, but then I wanted to break it up into a class. I wanted to do it in a class because this is my first attempt at a creating a class. I started to break it up and compile along the way. When I received 30+ errors I started to go back and try to correct them. I am still lacking some functionality but I wanted to get it to at least compile before I started to move on.
Reminder: This is my first attempt at making a class so please be easy, so to speak. Everyone tells me that doing things with classes is easier in the long run, but in the short horizon it is driving me insane. I am doing this with 3 files. Any assistance would be appreciated.
#ifndef _QUEUEMGR_H
#define _QUEUEMGR_H
# include <iostream>
# include <iomanip>
# include <string>
# include <fstream>
# include <queue>
# include <cstdlib>
usingnamespace std;
class QueueMgr
{
private:
queue<string> 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
# include <iostream>
# include <iomanip>
# include <string>
# include <fstream>
# include <queue>
# include <cstdlib>
# define FILE_IN "queue.txt"
usingnamespace std;
# include "QueueMgr.h"
ifstream input;
//OPEN/READ FILE
void QueueMgr::OpenFile(string fn)
{
//string fn;
input.open(FILE_IN);
if (!input)
{
cout << "\n" << "Can't open the specified file! " << FILE_IN;
cout << "\n" << "EXITING PROGRAM" << "\n";
exit(1);
}
else
{
getline(input, fn);
cout << "\n" << fn << "\n\n";
}
}
//ADDS ITEMS TO THE QUEUE
void QueueMgr::AddToQ(queue <string> qlist)
{
string line;
if (!input.eof())
{
getline(input, line);
qlist.push(line);
cout<<"--------------------------------------------------------------------------------\n\n"
<< line << " has been added to the queue. " << "\n\n"
<<"--------------------------------------------------------------------------------\n\n";
}
else
{
cout<<"--------------------------------------------------------------------------------\n\n"
<< "No more items in the file to add to queue. \n\n"
<<"--------------------------------------------------------------------------------\n\n";
}
//return (line);
}
//REMOVES/SERVES OUT FIRST ITEM IN QUEUE
void QueueMgr::ServeQ(queue <string> qlist)
{
if(qlist.empty() == true)
{
cout<<"--------------------------------------------------------------------------------\n\n"
<< setw(57) << "There is nothing in the queue. " << "\n"
<<"--------------------------------------------------------------------------------\n\n";
}
else
{
cout<<"--------------------------------------------------------------------------------\n\n"
<< "Now serving: " << qlist.front() << "\n\n"
<<"--------------------------------------------------------------------------------\n\n";
qlist.pop(); //REMOVE FIRST ITEM IN QUEUE
}
}
//SHOWS NUMBER OF ITEMS IN QUEUE
void QueueMgr::ShowQ(queue <string> qlist)
{
int num = qlist.size();
if(num == 0)
{
cout<<"--------------------------------------------------------------------------------\n\n"
<< setw(55) << "There are no items in the queue. \n\n"
<<"--------------------------------------------------------------------------------\n\n";
}
else
{
cout<<"--------------------------------------------------------------------------------\n"
<< "\n" << "Total items in the queue: " << num << "\n\n"
<<"--------------------------------------------------------------------------------\n\n";
}
}
//DISPLAYS WHO/WHAT IS AT THE FRONT OF THE QUEUE
void QueueMgr::FrontQ(queue <string> qlist)
{
int num = qlist.size();
if (num == 0)
{
cout<<"--------------------------------------------------------------------------------\n\n"
<< setw(55) << "There is nothing in the queue. " << "\n\n"
<<"--------------------------------------------------------------------------------\n\n";
}
else
{
cout<<"--------------------------------------------------------------------------------\n"
<< qlist.front() << " is at the front of the queue. " << "\n\n"
<<"--------------------------------------------------------------------------------\n\n";
}
}
//DISPLAYS WHO/WHAT IS AT THE BACK OF THE QUEUE
void QueueMgr::BackQ(queue <string> qlist)
{
int num = qlist.size();
if (num == 0)
{
cout<<"--------------------------------------------------------------------------------\n\n"
<< setw(55) << "There is nothing in the queue. " << "\n\n"
<<"--------------------------------------------------------------------------------\n\n";
}
else
{
cout<<"--------------------------------------------------------------------------------\n"
<< qlist.back() << " is at the back of the queue. " << "\n\n"
<<"--------------------------------------------------------------------------------\n\n";
}
}
//CLEAR THE QUEUE
void QueueMgr::ClearQ (queue <string> qlist)
{
if (qlist.empty() == true)
{
cout<<"--------------------------------------------------------------------------------\n\n"
<< setw(55) << "!!!The queue is already empty!!!" << "\n\n"
<<"--------------------------------------------------------------------------------\n\n";
}
else
{
do
{
qlist.pop();
}
while (qlist.empty() == false);
cout<<"--------------------------------------------------------------------------------\n\n"
<< setw(55) << "!!!The queue is now empty!!!" << "\n\n"
<<"--------------------------------------------------------------------------------\n\n";
}
}
# include <iostream>
# include <iomanip>
# include <queue>
# include <string>
# include <fstream>
# include <cstdlib>
usingnamespace std;
# include "QueueMgr.h"
int main()
{
QueueMgr myQueueManager("queue.txt");
int choice = 0;
cout << "1) Add an item to the queue \n"
<< "2) Serve out the first item in the queue \n"
<< "3) How many items are in the queue \n"
<< "4) What/who is at the front of the queue \n"
<< "5) What/who is at the back of the queue \n"
<< "6) Clear the queue \n\n"
<< "7) Exit the program \n\n"
<< "Pick a menu item ==> "; cin >> choice; cin.ignore(); cout << "\n\n";
while (choice !=7)
{
switch(choice)
{
case 1:
myQueueManager.AddToQ(qlist);
break;
case 2:
myQueueManager.ServeQ(qlist);
break;
case 3:
myQueueManager.ShowQ(qlist);
break;
case 4:
myQueueManager.FrontQ(qlist);
break;
case 5:
myQueueManager.BackQ(qlist);
break;
case 6:
myQueueManager.ClearQ(qlist);
break;
case 7:
cout<<"--------------------------------------------------------------------------------\n"
<< setw(58) << "!!!Exiting program!!!" << "\n\n"
<< "--------------------------------------------------------------------------------\n\n";
break;
}
}
return 0;
}
At this point I am generating 6 errors all in main related to:
error C2065: 'qlist' : undeclared identifier
I am sure there will be some other problems once these are ironed out but once again any assistance is appreciated or any assistance that may help me to better understand classes too would be great.
References make sense...I dont know why I changed that aspect from my original program seeing as that was how I had it. Also when it comes to defining qlist I am a bit confused. I defined it in the class as private, correct? Would moving it to public make any more sense or would I still need to do it in main?
I have been back and forth on this thing and I think i am confusing myself in this matter.
so in my class I have:
queue<string>qlist
I cannot redefine it in main by doing:
queue<string>qlist
right?
so then what am I defining it as in main?
looks like my week/weekend will consist of creating different simple classes(easier than my original program) to help me better understand what exactly I am not doing...4 hours of sleep starts to have you seeing code backwards I swear...lol
I've been having trouble sleeping lately myself, so I know how you feel. I missed that you have defined the qlist in your class. Since you have that object in your class, you don't need to pass it into your functions. Just change the signature to pass nothing. Make sense?
okay so I am in thought process now...follow me for a bit if you dont mind...
If I make those changes I then need to go back to my class and remove the same stuff correct?
make them consistent!
in making those changes I am then met with an interesting error for which I have not seen before which is nice since it is something different:
unresolved external symbol "public: __thiscall QueueMgr::~QueueMgr(void)" (??1QueueMgr@@QAE@XZ) referenced in function _main
unresolved external symbol "public: __thiscall QueueMgr::QueueMgr(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0QueueMgr@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
which at this point leaves me stumped. I am guessing one has to do with my destructor?
I'm not sure I follow you about removing the same stuff in your class. I think you are saying that the class functions should be changed in the class to reflect that they no longer take an argument, right?
Second problem: Yes it looks like the compiler is not able to resolve your destructor. Maybe a make clean and/or rebuild all will help? What OS is this on?
Makes sense which leads me to my next question(although I am reading up on it while I take a break and write this)...are they set up like a function...what is the point is the root of my question. Only because all of my code that actually runs the program are not in the constructor, destructor, or overloaded constructor. So what would go in there, and that's the gap in my understanding.
Erm, the constructor sets up the object, the destructor would destroy it. Have them as empty if you don't want to use them, but if you don't want to use them why did you go to the effort of defining an overloaded constructor?
1 2 3
QueueMgr() {}//default constructror, does not open a file
QueueMgr(string fn) {}//overloaded constructor, passes in the file name
~QueueMgr() {}//destructor closes the file
Changing the lines in the header file like so should remove those linker errors.
Unfortunately changing those lines in the header file did not remove the linker errors...I think I am going to have to resort to reading up a bit more and scraping this attempt and start over so as to not confuse myself as I think I have already done.
BTW...If I comment out the overloaded constructor and destructor I come up with one error and it is:
error 2664: 'QueueMgr::QueueMgr(const QueueMgr &)' cannot convert parameter 1 from 'const char [10]' to 'const QueueMgr &'