[英]more than one operator “>>” matches these operands
無論我在字符串流中使用“ >>”,都會給我該錯誤。 我不明白為什么會出現此錯誤,因為我只重載了>>以與AbsClass
一起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;
}
升
#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;
}
改變這個
// JAMES: ADDED THIS:
stringstream& operator>>(stringstream& stream, AbsClass& obj)
{
int buffer;
stream >> buffer;
obj.setNum( buffer );
return stream;
}
對此
// JAMES: ADDED THIS:
istream& operator>>(istream& stream, AbsClass& obj)
{
int buffer;
stream >> buffer;
obj.setNum( buffer );
return stream;
}
您不會只為istream重載stringstream的operator >>。 然后它將適用於任何輸入流,包括stringstream。 不能完全確定這是否是問題的根源,但絕對是正確的做法。
我忘了給AbsClass
添加標題保護。 這樣就解決了。
除了標題保護之外,還需要使operator>>
inline
,或將實現移動到.cpp,否則將獲得多個定義的符號鏈接錯誤。
也不要在標題中使用名稱空間
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.