0

Wherever I use ">>" with stringstream it gives me that error. I don't see why I should be getting this error because I have only overloaded >> to work with AbsClass, not with any other types.

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

k

#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;
}
JamesGold
  • 795
  • 2
  • 8
  • 24

3 Answers3

0

Change this

// JAMES: ADDED THIS:
stringstream& operator>>(stringstream& stream, AbsClass& obj)
{
  int buffer;
  stream >> buffer;

  obj.setNum( buffer );

  return stream;
}

to this

// JAMES: ADDED THIS:
istream& operator>>(istream& stream, AbsClass& obj)
{
  int buffer;
  stream >> buffer;

  obj.setNum( buffer );

  return stream;
}

You don't overload operator>> for stringstream just for istream. Then it will work for any input stream including stringstream. Not completely sure if that is the cause of the issue but definitely the right thing to do.

john
  • 85,011
  • 4
  • 57
  • 81
  • It will work with stringstream. If you overload for `istream` it will for for **any** input stream, `cin`, `ifstream`, `fstream`, `istringstream`, `stringstream`, all of them. – john Apr 27 '13 at 22:49
  • Okay. Changed it to istream, but the problem persists. – JamesGold Apr 27 '13 at 23:06
  • Could you post the complete error message. Cut and paste a few lines above it and a few lines below it. – john Apr 27 '13 at 23:08
0

I forgot to add header guards to AbsClass. That fixed it.

JamesGold
  • 795
  • 2
  • 8
  • 24
  • Yes I was just about to say, but you should also declare your operator>> inline, it is in a header file. `inline istream& operator>>(istream& stream, AbsClass& obj) { ...` Unless you declare it inline you are liable to get 'multiply defined symbol' linker errors. – john Apr 27 '13 at 23:10
0

Apart from header guards, you also need to either make your operator>> inline, or move implementation to the .cpp or you will get multiply defined symbol link errors.
Also don't use namespaces in headers

Community
  • 1
  • 1
alexrider
  • 4,449
  • 1
  • 17
  • 27
  • Each .cpp that includes header will define it's own operator>> breaking [ODR](http://stackoverflow.com/questions/4192170/what-exactly-is-one-definition-rule-in-c) – alexrider Apr 27 '13 at 23:18
  • When I put 'operator>>' in AbsClass.cpp, the main cpp file says 'no operator ">>" matches these operands.' I edited AbsClass.cpp into my post. – JamesGold Apr 27 '13 at 23:31
  • @JamesGold You need to move only definition, declaration should remain in a header file. – alexrider Apr 27 '13 at 23:33