繁体   English   中英

多个运算符“ >>”与这些操作数匹配

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM