Question

I've spent the last 30 minutes trying to figure out what is wrong with this:

From .h file:

// H/T sent. d-linked list Set

#ifndef SET_H
#define SET_H
#include <iostream>
#include <string>

using namespace std;
typedef string ELEMENT_TYPE;  // a set for string elements

class Set{
private:
    struct Elem {
        ELEMENT_TYPE info;
        Elem *prev, *next;
    };
    Elem *_head, *_tail;
    int _size;

    void copyCode(const Set & v);

    void destructCode();

    ostream& dump(ostream& out, const Set &v);

public:
    Set();

    Set(const Set &rhs);

    ~Set();

    Set& operator=(const Set &rhs);

    bool insert(ELEMENT_TYPE);

    bool erase(ELEMENT_TYPE);

    void clear();

    int size() const { return _size; }

    bool find(ELEMENT_TYPE) const;

    class Iterator{
    private:
    Elem * _cur;

    public:
        Iterator(){}
        explicit Iterator( Elem* );

    Iterator operator++( int );
    Iterator operator++();
        Iterator operator--( int);
    Iterator operator--();

    bool operator==( const Iterator& rhs );
    bool operator!=( const Iterator& rhs );

    ELEMENT_TYPE& operator*();

    ostream& operator<< ( ostream& );

    };

    Iterator begin() const; 
    Iterator end() const; 
    friend ostream& operator<< (ostream&, Set&);
};

bool operator==(const Set&, const Set&);
bool operator!=(const Set&, const Set&);
Set operator&(const Set&, const Set&);
Set operator|(const Set&, const Set&);

#endif

From .cpp file:

string& Set::Iterator::operator*(){

    return _cur -> info;
}


ostream& Set::Iterator::operator<< ( ostream& os ){

    os << _cur -> info << "\n";

    return os;
}

From test.cpp:

Set s1;

s1.insert( "1" );
s1.insert( "2" );
s1.insert( "3" );

cout << "Hi\n";

Set::Iterator it = s1.begin();
while( it != s1.end() ){
    cout << *it;
    it++;
}

cout << "Bye\n";

To me, this looks fine and like every operator<< I have made before, however when I run my test.cpp file where I put my code through it's paces I get:

Hi
321Bye

This is obviously not the information that I have provided in my operator<< definition, and I have also tried replacing the value accessing with a dummy output like "hi\n"; to very little success. This leads me to believe that I have defined it incorrectly and it is using some generic string output operator.

I'm sure this is a very simple problem, but I don't have the coveted second pair of eyes easily accessible.

Edit: Some are remarking that the code is perfect (blush) and that the question is unsolvable, but I do not know what I am missing, so I included the complete header file. I did not include the .cpp because of obvious space reasons. If you think the issue could be in a certain area I'll happily post my defintions.

The question is that the output does not contain newlines, which suggests that the operator is not being used at all. Why is that / what is missing from the operator that is not overloading it correctly (Thanks JBently)

Thanks!

Was it helpful?

Solution

You can`t implement operator<< as a method of class, it has to be a global function!

Your operator << doesn`t work at all, because in method implementation always first implicit argument is this so you implementation is analog to the function with the following signature:

ostream& operator<< (this, ostream& );

but to make operator << work - first argument should be ostream& implement operator as global function and you issue will be solved:

ostream& operator<< ( ostream& str, iterator& iter );

in this part of the code you simply dereference iterator (operator* is invoked)

Set::Iterator it = s1.begin();
while( it != s1.end() ){
    cout << *it;
    it++;
}

if you need to call operator<< rewrite it:

Set::Iterator it = s1.begin();
while( it != s1.end() ){
    cout << it;
    it++;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top