C++ Class/Queue problem

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.

QueueMgr.h
QueueMgr.cpp
Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef _QUEUEMGR_H
#define _QUEUEMGR_H

# include <iostream>
# include <iomanip>
# include <string>
# include <fstream>
# include <queue>
# include <cstdlib>

using namespace 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 


From there I have my QueueMgr.cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# include <iostream>
# include <iomanip>
# include <string>
# include <fstream>
# include <queue>
# include <cstdlib>

# define FILE_IN "queue.txt"


using namespace 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";

}

}


And last but not least I have my Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

# include <iostream>
# include <iomanip>
# include <queue>
# include <string>
# include <fstream>
# include <cstdlib>

using namespace 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.
Hey Mr Smooth,

In main.cpp, you don't have qlist defined.

Also, it if you have learned about references, your functions should take references instead of objects:
1
2
3

void QueueMgr::AddToQ(queue <string> &qlist)
Thanks kooth,

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?
You're welcome!

Since you are passing it into the functions, you must define 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?
If I am understanding you correctly within my functions just have:

void QueueMgr::ClearQ ()

instead of:
void QueueMgr::ClearQ (queue <string> &qlist)



Yes!
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?
Last edited on
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?
Yeah I tried a clean and rebuild only to get the same errors. I am running Vista
Where are you defining the constructor and destructor functions? (in the code in this thread you're not, which is what the errors are)
Last edited 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 &'
That's because at the start of Main you're calling a constructor with a string.
Topic archived. No new replies allowed.