Deque Question

I am getting an error code and I don't know why. If anyone could help I would appreciate it.

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
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <queue>
using namespace std;

typedef pair<int,int> process; //process type
queue<process> TaskQ; //Queue contains all tasks
queue<int> CPUunitQ; // Queue contains all available CPU

void loadTaskQ(string filename);

int main()
{
	string filename;
	cout<<"Please input the datafile: ";
	cin>>filename;
	loadTaskQ(filename);
	return 0;
}

void loadTaskQ(string filename)
{
// This function loads the data from filename into global queue TaskQ
// each elementin TaskQ is a pair <int,int> (which has been typedef as process type)

	ifstream inFile; // declare file stream variable
	inFile.open( filename.c_str() ); // open data file 
	if ( inFile.fail() ) // check to see if file will open, terminate the program if file failed to open                                  
        {
        cout << "Cannot open the input file." << endl;
        exit( 1 );
        }
	while( !inFile.eof() ) // read in data until the end of the file
	{
		inFile >> TaskQ.push(process.first); // error
		inFile >> TaskQ.push(process.second); // error
	inFile.close(); // close input file
        }
}
// error is illegal use of type as an expression 


The txt input file looks like this:
1 2
2 2
3 4
4 3
5 6
.
.
.
19 4 end of file.

Thanks for your help.
What is line 37 supposed to do?

You have to read into a variable, then call push with that variable, but only if the read was successful.
So I would make a variable like
TaskQ task
Then read in from file to task?
1
2
inFile << task.push(process.first); // using first since this a pair
inFile << task.push(process.second); // using second since this is a pair 
closed account (D80DSL3A)
process is a type name , not an instance of that type. Declare an instance of process, read values into it, then push it into task. You also seem to be closing the file a bit early (line 39), right after reading the 1st pair.
1
2
3
4
5
6
7
8
process temp;
while( !inFile.eof() ) // read in data until the end of the file
{
	inFile >>temp.first;
	inFile >> temp.second;
        task.push(temp);
}
inFile.close(); // close input file after reading all data 
Thanks again fun2Code your example worked great.
Last edited on
Topic archived. No new replies allowed.