多个运营商“>>”匹配这些操作数

时间:2013-04-27 22:40:18

标签: c++ operator-overloading

无论我在哪里使用“>>”使用stringstream它给了我那个错误。我不明白为什么我应该收到此错误,因为我只是重载了>>使用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;
}

1

#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;
}

3 个答案:

答案 0 :(得分:0)

更改此

// 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;
}

您不会重载运算符&gt;&gt; for stringstream只是为了istream。然后它适用于包括stringstream在内的任何输入流。不完全确定这是否是问题的原因,但绝对是正确的事情。

答案 1 :(得分:0)

我忘了向AbsClass添加标题保护。修好了。

答案 2 :(得分:0)

除了标题保护之外,您还需要制作operator>> inline,或将实施移至.cpp,否则您将获得多重定义的符号链接错误。
也不要use namespaces in headers