Problems with expression.cpp

Having a bit of trouble with accessibility in this program. I know it's probably something simple that I am missing. Also, unclear on how to do the files. Thanks for any help. The assignment is posted below.

Filenames are:

QNodeType.h - the "node type" struct for queue
stack.h - stack template class declaration.
queue.h - queue template class declaration.
expression.h - declaration file for the expression class
expression.cpp - implementation file for expression
in2post.cpp - driver program
The names of the input and output files will be provided on the command line. As in:

a.out inputfile outputfile

Use the following UML class diagrams:
QNodeType.h
QNodeType<T>
item: T
next: QNodeType<T>*

queue.h
queue<T>
-front_: QNodeType<T>*
-back_: QNodeType<T>*
-count: size_t
+queue()
+queue(const queue<T>&)
+~queue()
+operator=(const queue<T>&): const queue<T>&
+push(const T&): void
+pop(): void
+front() const: const T&
+back() const: const T&
+size() const: size_t
+empty() const: bool
-destroy(): void
-copy(const queue<T>&): void

stack.h
stack<T>
-items: vector<T>
+stack(size_t =10)
+stack(const stack<T>&)
+push(const T&): void
+pop(): void
+size() const: size_t
+empty() const: bool
+top() const: const T&

expression.h
-ifix: string
-pfix: string
+last: bool
operator>>(istream&, expression&): istream&
operator<<(ostream&, const expression&): ostream&
-convertToPostfix(): void
-precedence(char, char) const: bool
+expression()

The input file will consist of one or more correct infix expressions separated by semi-colons. The last expression will end with a period. The file may contain additional characters after that period but you should not read past the period. The input will consist of the upper-case letters A-Z; the operators +,-,*,/; the left and right parentheses, and the period and semicolon. At least in the 'expression' portion of the input file. Whitespace is allowed between individual symbols in the infix expression. Sample input file would look something like:

A+B;
A
+ C
;
A*B.
The output for the above file should look like:

Infix: A+B;
Postfix: AB+
Infix: A+C;
Postfix: AC+
Infix: A*B.
Postfix: AB*
Expressions are read in and queued and after all expressions in the input file have been queued each is processed in turn.
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
///////////////////////////////////////////////////////////////////////////////
//
//  Kimberly Rappold
//  Program #4
//  Due: 4/11/2020
//
//  Description:
//  This is a program that takes an input file consisting of one or more 
//	correct infix expressions that are separated by a semicolon with the last 
//	expression that ends with a period, then reads and queues the expressions.
//	After all exression in the input file has been queued, each expression
//	is processed in turn. 
//
//  Assumptions:
//  
///////////////////////////////////////////////////////////////////////////////

#ifndef STACK_H
#define STACK_H
#include <iostream>
#include <vector>
#include "queue.h"

template <class T>
class stack {
public: 
	stack(size_t = 10); 
	//////////////////////////////////////////////////////////////////////////////
	//
	//  Function: stack(size_t = 10);
	//
	//  Description:
	//	Constructor. Create an array of the size to hold the stack elements. The 
	//	default size is 10. 
	//	
	//	Postcondition: 
	//	The variable list contains the base address of the array.
	//////////////////////////////////////////////////////////////////////////////
	
	stack(const stack<T>&); 
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: stack(const stack<T>&);
	//
	//  Description:
	//	Copy constructor. 
	//	
	///////////////////////////////////////////////////////////////////////////

	void push(const T& item);
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: void push(const T& item);
	//
	//  Description:
	//	Function to add item to the stack. 
	//
	//	Precondition: 
	//	The stack exists and is not full. 
	//
	//	Postcondition: 
	//	The stack is changed and item is added to the top of the stack. 
	//	
	///////////////////////////////////////////////////////////////////////////

	void pop();
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: void pop();
	//
	//  Description:
	//	Function to remove the top element of the stack. 
	//
	//	Precondition: 
	//	The stack exists and is not empty. 
	//
	//	Postcondition: 
	//	The stack is changed and the top element is removed from the stack. 
	//	
	///////////////////////////////////////////////////////////////////////////

	size_t size() const; 
	///////////////////////////////////////////////////////////////////////////////
	//
	//  Function: size_t size() const;
	//
	//  Description:
	//  This is a function to return the number of nodes in the list. 
	//	
	//	Postcondition: 
	//	 
	///////////////////////////////////////////////////////////////////////////////

	bool empty() const; 
	///////////////////////////////////////////////////////////////////////////////
	//
	//  Function: bool empty() const;
	//
	//  Description:
	//  This is a function to determine whether the stack is empty. 
	//	
	//	Postcondition: 
	//	Returns true if the stack is empty, otherwise returns false.
	///////////////////////////////////////////////////////////////////////////////

	const T& top() const; 
	///////////////////////////////////////////////////////////////////////////////
	//
	//  Function: const T& top() const;
	//
	//  Description:
	//  This is a function to return the top element of the stack.
	//
	//	Precondition: 
	//	The stack exists and is not empty. 
	//	
	//	Postcondition: 
	//	If the stack is empty, the program terminates; otherwise, the top 
	//	element of the stack is returned. 
	///////////////////////////////////////////////////////////////////////////////
private: 
	vector<T> items;
};

template <class T> 
stack<T>::stack(size_t n) : queue<T>(n) {
}
template <class T> 
stack<T>::stack(const stack<T>& source) {
	copy(source);
	count = source.count;
}
template <class T> 
void stack<T>::push(const T& item) {
	if (front_ == nullptr) {

		front_ = new QNodeType<T>;
		/*create new node*/

		front_->item = item;
		/*inserts item at the top of the stack*/

		front_->next = nullptr;
		/*initialize the next field to nullptr*/

		back_ = front_;
		/*updates the value*/

	}
	else {
	/*add new node to the back*/

		back_->next = new QNodeType<T>;
		/*add new node*/

		back_ = back_->next;

		back_->item = item;
		/*add item to the back node*/
	}
	++count;
	/*increments the count*/
}
template <class T> 
void stack<T>::pop() {
	if (!empty()) {

		QNodeType<T>* temp = front_;
		/*make temp point to the front node*/

		front_ = front_->next;
		/*advance front*/

		delete temp;
		/*delete the first node*/

		if (front_ == nullptr) {
			/*if after deletion the queue is empty*/

			back_ = nullptr;
			/*set the back to nullptr*/
		}
		--count;
		/*decrement the count*/
	}
}
template <class T> 
size_t stack<T>::size() const {
	return count; 
}
template <class T> 
bool stack<T>::empty() const {
	return (front_ == nullptr);
}
template <class T> 
const T& stack<T>::top() const {
	if (!empty()) {
		return front_->item;
	}
}
#endif 
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
#include "expression.h"
#include "queue.h"
#include "stack.h" 
#include <fstream> 
#include <iostream> 
#include <queue>

using namespace std; 

int main() {
	ifstream fin; 
	ofstream fout; 
	expression exp; 
	std::queue<char> q;

	fin.open("input.txt");
	fout.open("output.txt"); 
	
	while (!exp.last) {
		fin >> exp;
		q.push(exp);
		q.push(exp);
	}
	fin.close();
	while (!q.empty()) {
		exp = q.front(); 
		fout << exp;
		q.pop(); 
	}
	fout.close();
	return 0; 
}
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
#include <stack> 
#include "expression.h"
#include <iostream> 
#include <fstream> 

using namespace std; 

expression::expression() {
	ifix = ""; 
	pfix = ""; 
	last = false; 
}
bool expression::precedence(char s, char c) const {
	if (s == '(' || s == '$') return false; 
	if (s == '*' || s == '/') return true; 
	return (c = '+' || c == '-');
}
void expression::convertToPostFix() {
	std::stack<char> s;

	s.push('$');
	pfix = "";

	for (size_t i = 0; i < ifix.size() - 1; ++i) {
		switch (ifix[i]) {
		case'(':s.push(ifix[i]);
			break;
		case')':while (s.top() != '(') {
			pfix += s.top();
			s.pop();
		}
			   s.pop();
			   break;
		case'+':
		case'-':
		case'*':
		case'/': while (precedence(s.top(), ifix[i])) {
			pfix += s.top();
			s.pop();
		}
			   s.push(ifix[i]);
			   break;
		default: pfix += ifix[i];
		}
	}
	while (s.top() != '$')
		pfix += s.top();
}
std::istream& operator >>(std::istream& in, expression& exp) {
	char sym; 
	exp.ifix = "";
	do {
		in >> sym; 
		exp.ifix += sym; 
	} while (sym != '.' && sym != ';');
	if (sym == '.') exp.last = true; 
	exp.convertToPostFix(); 
	return in;
}
std::ostream& operator << (std::ostream& out, expression& exp) {
	out << "Infix: " << exp.ifix << std::endl;
	out << "Postfix: " << exp.pfix << std::endl;
	return out; 
}
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
///////////////////////////////////////////////////////////////////////////////
//
//  Kimberly Rappold
//  Program #4
//  Due: 4/11/2020
//
//  Description:
//  This is a program that takes an input file consisting of one or more 
//	correct infix expressions that are separated by a semicolon with the last 
//	expression that ends with a period, then reads and queues the expressions.
//	After all exression in the input file has been queued, each expression
//	is processed in turn. 
//
//  Assumptions:
//  
//
///////////////////////////////////////////////////////////////////////////////

#ifndef EXPRESSION_H
#define EXPRESSION_H
#include "QNodeType.h"
#include "queue.h" 
#include "stack.h" 
#include <iostream> 
#include <fstream> 
#include <string> 

class expression {
public: 
	friend std::istream& operator >>(istream&, expression&); 
	friend std::ostream& operator <<(ostream&, const expression&);
	expression(); 
	void convertToPostFix();
	bool precedence(char, char) const;
	
private:
	string ifix;
	string pfix;
	bool last;
};
#endif  
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
///////////////////////////////////////////////////////////////////////////////
//
//  Kimberly Rappold
//  Program #4
//  Due: 4/28/2020
//
//  Description:
//  This is a program that takes an input file consisting of one or more 
//	correct infix expressions that are separated by a semicolon with the last 
//	expression that ends with a period, then reads and queues the expressions.
//	After all exression in the input file has been queued, each expression
//	is processed in turn. 
//
//  Assumptions:
//  
//
///////////////////////////////////////////////////////////////////////////////

#ifndef QUEUE_H
#define QUEUE_H
#include <cstdlib>
#include "QNodeType.h"
template <class T> 
class queue {
public:
	queue();
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: queue();
	//
	//  Description:
	//  This function is the Default Constructor.  
	//
	///////////////////////////////////////////////////////////////////////////
	
	queue(const queue<T>&);
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: queue(const queue<T>&);
	//
	//  Description:
	//  This function is the Copy Constructor.  
	//
	///////////////////////////////////////////////////////////////////////////

	virtual ~queue();
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: virutal ~queue();
	//
	//  Description:
	//  This function is the Destructor.  
	//
	///////////////////////////////////////////////////////////////////////////

	const queue<T>& operator = (const queue<T>&); 
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: queue();
	//
	//  Description:
	//  This function is to overload the assignment operator.   
	//
	///////////////////////////////////////////////////////////////////////////

	void push(const T&);
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: void push(const T&);
	//
	//  Description:
	//	Function to add item to the queue. 
	//
	//	Precondition: 
	//	The queue exists and is not empty. 
	//
	//	Postcondition: 
	//	The queue is changed and item is added to the list. 
	//
	///////////////////////////////////////////////////////////////////////////

	void pop();
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: void pop();
	//
	//  Description:
	//   Function to remove the first item of the queue. 
	//
	//	Precondition: 
	//	The queue exists and is not empty. 
	//
	//	Postcondition: 
	//	The queue is changed adn the first element is removed from the queue. 
	//
	///////////////////////////////////////////////////////////////////////////

	const T& front() const; 
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: const T& front() const;
	//
	//  Description:
	//  This function is to return the first element of the queue to an empty
	//	state. 
	//
	//	Precondition: 
	//	The queue exists and is not empty. 
	//
	//	Postcondition: 
	//	If the queue is empty, the program terminates; otherwise, the first
	//	element of the queue is returned. 
	//
	///////////////////////////////////////////////////////////////////////////

	const T& back() const; 
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: const T& back() const;
	//
	//  Description:
	//  This function is to return the last element of the queue. 
	//
	//	Precondition: 
	//	The queue exists and is not empty. 
	//
	//	Postcondition: 
	//	If the queue is empty, the program terminates; otherwise, the 
	//	last element of the queue is returned. 
	//
	///////////////////////////////////////////////////////////////////////////

	size_t size() const;
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: size_t size() const;
	//
	//  Description:
	//  This is a function to return the number of nodes in the list. 
	//	
	//	Postcondition: 
	//	The value of count is returned. 
	//
	///////////////////////////////////////////////////////////////////////////

	bool empty() const; 
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: bool empty() const; 
	//
	//  Description:
	//  This is a function to determine whether the queue is empty.
	//	
	//	Postcondition: 
	//	Returns true if the queue is empty, otherwise it returns false. 
	///////////////////////////////////////////////////////////////////////////

private: 

	void destroy();
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: void destroy();
	//
	//  Description:
	//  This is a function to destroy the queue. 
	//	
	//	Postcondition: 
	//	The count within the queue is equal to 0. 
	///////////////////////////////////////////////////////////////////////////

	void copy(const queue<T>&);
	///////////////////////////////////////////////////////////////////////////
	//
	//  Function: void copy(queue<T>&);
	//
	//  Description:
	//  This is a function to make a copy of the queue. 
	//	
	//	Postcondition: 
	//	A copy queue is created and assigned. 
	///////////////////////////////////////////////////////////////////////////s

	QNodeType<T>* front_;
	/*pointer to the first element of the queue.*/

	QNodeType<T>* back_;
	/*pointer to the last element of the queue.*/

	size_t count;
	/*variable to store the number of elements in the queue.*/
};

template <class T> 
queue<T>::queue() {
	front_ = back_ = nullptr;
	/*sets all to end of node*/

	count = 0;
	/*sets count to 0*/
}
template <class T> 
queue<T>::queue(const queue<T>& source) {
	copy(source); 
	count = source.count;
}

template <class T> 
queue<T>::~queue() {
	destroy();
}
template <class T> 
const queue<T>& queue<T>::operator =(const queue<T>& source) {
	if (this != &source) {
		destroy(); 
		copy(source);
	}
	return *this;
}

template <class T> 
void queue<T>::push(const T& item) {
	if (front_ == nullptr) {

		front_ = new QNodeType<T>;
		/*create new node*/

		front_->item = item;
		/*store the item*/

		front_->next = nullptr;
		/*initialize the next field to nullptr*/

		back_ = front_;
		
	}
	else {
	/*add new node to the back*/

		back_->next = new QNodeType<T>;
		/*add new node*/

		back_ = back_->next;

		back_->item = item;
		/*add item to the back node*/
	}
	++count;
	/*increments the count*/
}
template <class T> 
void queue<T>::pop() {
	if (!empty()) {

		QNodeType<T>* temp = front_;
		/*make temp point to the front node*/

		front_ = front_->next;
		/*advance front*/

		delete temp;
		/*delete the first node*/

		if (front_ == nullptr) {
		/*if after deletion the queue is empty*/

			back_ = nullptr;
			/*set the back to nullptr*/
		}
		--count;
		/*decrement the count*/
	}
}

template <class T> 
const T& queue<T>::front() const {
	if (!empty()) {
		return front_->item; 
	}
}
template <class T>
const T& queue<T>::back() const {
	if (!empty()) {
		return back_->item;
	}
}

template <class T> 
size_t queue<T>::size() const {
	return count; 
}

template <class T>
bool queue<T>::empty() const {
	return (front_ == nullptr);
}

template <class T> 
void queue<T>::destroy() {
	while (!empty()) {
		pop();
	}
}

template <class T> 
void queue<T>::copy(const queue<T>&source) {

	QNodeType<T>* temp = source.front_;
	/*make temp point to the front*/

	front_ = back_ = nullptr;
	/*sets front_ and back_ to nullptr*/

	if (temp != nullptr) {

		front_ = new QNodeType<T>;
		/*create the node*/

		front_->next = nullptr;
		/*initialize the next to nullptr*/

		back_ = front_;

		temp = temp->next;
		/*advance temp*/

		while (temp != nullptr) {

			back_->next = new QNodeType<T>;
			/*set new node to the last node*/

			back_ = back_->next;
			/*advance back*/

			temp = temp->next;
			/*advance temp*/
		}
		back_->next = nullptr;
		/*initialize the next field to nullptr*/
	}
	else {
		front_ = back_ = nullptr;
		/*set front_ and back_ nodes to nullptr*/
	}
	count = source.count;
}
#endif 
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
///////////////////////////////////////////////////////////////////////////////
//
//  Kimberly Rappold
//  Program #4
//  Due: 4/11/2020
//
//  Description:
//  This is a program that takes an input file consisting of one or more 
//	correct infix expressions that are separated by a semicolon with the last 
//	expression that ends with a period, then reads and queues the expressions.
//	After all exression in the input file has been queued, each expression
//	is processed in turn. 
//
//  Assumptions:
//  The QNodeType* next does not contain QNodeType<T>*link due to Visual Studio 
//	indicating a compiler error stating that the template had already been 
//	defined. Therefore, I had to define QNodeType<T> *next as NodeType *next. 
//
///////////////////////////////////////////////////////////////////////////////
#ifndef H_QNodeType
#define H_QNodeType
#include <iostream>

//Definition of the Node
template <class T>
struct QNodeType {
	T item;
	QNodeType* next;
};
#endif 
Hello kmheflin712,

While I get your program loaded up to see what it is doing. Here is what I noticed so far.

In the file that contains "main" you have:
1
2
3
4
5
6
7
8
9
int main()
{
	ifstream fin;
	ofstream fout;
	expression exp;
	std::queue<char> q;

	fin.open("input.txt");
	fout.open("output.txt");

On line 8 you open the file stream for input, but how do you know it is open?

When you open a file stream for input it is a must that you check that it worked. This is a way of shortening what you did and checking the file stream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	ifstream fin("input.txt");
	ofstream fout;
	expression exp;
	std::queue<char> q;

	fout.open("output.txt");

	if (!fin)
	{
		std::cout << "\n    File \"input.txt\" did not open\n";

		return 1;
	}

Line 3 not only defines the file stream, it also opens the file when it is constructed. You may see this method used often in the code that is posted here.

The same can be done for the output file stream, but it is not as important because if the file does not exist it will be created first. Should there be a path to the file then checking the file becomes of more use.

The "return" statement in the "if" statement will leave the program because there is no reason to continue with a file that is not open. Otherwise your code will continue as if nothing is wrong. It is just that you will not be able to read anything from the file.

Another thing that I noticed. In the "stack.h file you have:
1
2
3
4
5
6
#ifndef STACK_H
#define STACK_H

// Code here.

#endif 

This header guard is good, but I did not see this in all your header files. You should use this in all your header files.

For a large program like this you can post it in several replies as you did. For a large file I have seen https://pastebin.com/ used and a link to it put in the posts or replies.

Andy
Hello kmheflin712,

Disregard what I said earlier. My bad.

Your header files do contain header guards, I must have been looking at a ".cpp" and did not realize it at the time.

When I compiled the program I had 90 errors. This might have been from something in what you posted that does not show up when I copied and pasted it.

One problem I did find is in the header file "expression.h" you defined
friend std::istream& operator >>(istream&, Expression&);

This should be:
friend std::ifstream& operator >>(ifstream&, Expression&);
You are missing the "f" after the "i" and "o".

Not sure if you need the "&" after "Expression" or not.

Class names should start with a capital letter, but you are free to use whatever you want. I made this change to see if I could get rid of some of the errors. The class name is more proper, but it did not help.

Andy
Hello kmheflin712,

Sometimes debugging the program is best done in small steps just like writing the program.

I decided to start with "main" and get as much as I could working. One of the things I noticed:

The names of the input and output files will be provided on the command line. As in:

a.out inputfile outputfile


I am wondering what "a.out" is here? Should this be the name of the program or is it something else?

This is required for the program, but you have not dealt with it yet. Since you have not mentioned what IDE you are using or how you are running the program I am not sure what you can do when you run the program for testing.

Then there is the "#include " files:
1
2
3
4
5
6
7
#include "expression.h"
#include "queue.h"  // <--- Header file you wrote.
#include "stack.h" 
#include <fstream> 
#include <iostream> 
#include <queue>  // <--- STL header file. And why do you think you need it?
#include <string> 

The comment on line 6 needs to be answered.

Then you have: std::queue<char> q; // <--- Do you know which "queue" you are using? .

Then the code:
1
2
3
4
5
6
while (!exp.last)
{
	fin >> exp;
	q.push(exp);
	q.push(exp);
}

Has at least 3 problems.

First is the while condition. "last" is a private member variable of the class and thus not able to be accessed directly. You will need a public member functions to return the value of "last". I am think something like while (!(exp.GetLast())). The () around the function call may be needed. I have not tested that part yet.

Next fin >> exp;. Here you are trying to read something into an entire class. Even if you opened the file stream in "binary" mode it still would not work. Not sure what your intention is here, but what you need is a "set" function to change the values of the private variables.

For q.push(exp);. Here "q" was defined as a "char", but you are trying to put an entire class into a a "char". This does not work. Since this does not compile I have not had the need to work on the next while loop.

I also noticed in the "expression.cpp" file you have:
1
2
3
4
5
6
7
#include <stack>  // <--- STL header file. And why do you think you need it?
#include "expression.h"
#include <iostream> 
#include <fstream> 


std::stack<char> s;  // <--- Do you know which "stack" you are using? 

Like using "<queue>" in "main" this is going to cause a problem.

Andy
Respectfully Andy, for anyone reading this post who may not know any better, I would like to provide some clarification on one of the statements you have made (not to snipe, but just in case it is misunderstood).

Class names should start with a capital letter, but you are free to use whatever you want. I made this change to see if I could get rid of some of the errors. The class name is more proper, but it did not help.


Naming conventions vary and a class name doesn’t need to be capitalised in order to be ‘proper’ (as a fact in its own right). The fact a class name isn’t capitalised doesn’t mean the code won’t compile, link and function properly (again, as a fact in its own right).

The key point is that the style and convention used throughout a codebase should be consistent and clear. This makes the code more readable and easier to understand. In some cases, coding conventions can have a functional and performance impact on the resultant software too.

One common convention (across differing style guides and code conventions) is to capitalise the first letter of a class name, however this isn’t a requirement for every codebase.

The Standard C++ Foundation website (https://www.isocpp.org) has an F.A.Q page dedicated to coding standards and conventions, linking to some further useful resources on this topic (such as industrial coding standards which have been well received by certain members of the Foundation). This page can be found here: https://isocpp.org/wiki/faq/coding-standards

Jack
Topic archived. No new replies allowed.