无论我在哪里使用带有字符串流的“>>”,它都会给我这个错误。我不明白为什么我应该得到这个错误,因为我只重载了 >> 来使用AbsClass
,而不是使用任何其他类型。
#include "Queue.h"
#include "AbsClass.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
int main()
{
ifstream read;
try
{
read.open( "TopicGin.txt" );
if (!read)
throw QueueException();
}
catch ( QueueException& )
{
cerr << "Could not find input file.\n";
char endchar = getchar();
if(endchar == '\nn')
return 0;
}
string line;
getline( read, line );
stringstream stream;
stream << line;
int buffer;
Queue <int> q1;
while ( stream >> buffer )
{
q1.enqueue( buffer );
cout << buffer << ' ';
cout << "count is ";
q1.displayCount();
}
cout << q1.peek();
q1.dequeue();
cout << q1.peek();
Queue<int> q2 = q1;
q2.peek();
cout << "\n The contents of q2 with queue size " << q2.getSize() << " are: \n";
q2.displayArray();
//NEED EXCEPTIONS FOR THIS PART JUST SEETING UP
cout << " Attempt to create a queue of int with an invalid size.\n";
Queue<int> q3(-1);
cout << "Create queue object of double q4 with a size of 14.\n";
Queue<double> q4(14);
cout << "Read in values from the input file. \n";
stream.clear();
double dBuf;
getline ( read, line );
stream << line;
while ( stream >> dBuf )
{
q4.enqueue( dBuf );
cout << dBuf << ' ';
cout << "count is ";
q4.displayCount();
}
cout << fixed << setprecision(2);
cout << q4.peek();
q4.dequeue();
cout << q4.peek();
cout << "Create Queue object of 5 which is a copy of q4\n";
Queue<double> q5 = q4;
q5.peek();
q5.displayArray();
//EXCEPTIONS AGAIN DLFKJALDKAJ
cout << "Attempt to peek an empty queue. \n";
Queue<double> q6;
q6.peek();
cout << "Create Queue object of AbsClass q7 with default size. \n";
cout << "Read in values from input file \n";
Queue<AbsClass> q7;
cout << "Read in values from the input file. \n";
AbsClass AbsBuf;
getline ( read, line );
stream.clear();
stream << line;
while ( stream >> AbsBuf)
{
q7.enqueue( AbsBuf );
cout << AbsBuf << ' ';
cout << "count is ";
q7.displayCount(); 0
}
return 0;
}
l
#include<iostream>
#include<cmath>
#include <string>
// JAMES: ADDED THIS:
#include <sstream>
using namespace std;
// JAMES: ADDED THIS:
stringstream& operator>>( stringstream& stream, AbsClass& obj );
class AbsClass
{
public:
AbsClass(int val = 0){num = abs(val);} // Inlined constructor
int getNum()const {return num;}
// JAMES: ADDED THIS:
void setNum( int newNum ) { num = abs(newNum ); }
private:
int num;
};
// JAMES: ADDED THIS:
stringstream& operator>>(stringstream& stream, AbsClass& obj)
{
int buffer;
stream >> buffer;
obj.setNum( buffer );
return stream;
}
ķ
#include "AbsClass.h"
#include <sstream>
using namespace std;
// JAMES: ADDED THIS:
istream& operator>>( istream& stream, AbsClass& obj );
istream& operator>>( istream& stream, AbsClass& obj )
{
int buffer;
stream >> buffer;
obj.setNum( buffer );
return stream;
}