Pergunta

I'm working on a hash table program involving linear probing. I'm trying to print out the vector of Symbol class objects but I'm running into two errors every time I try to do it. I've posted these errors below:

LinearProbing.h: In instantiation of 'void HashTable<HashedObj>::print() [with HashedObj = Symbol]':
Driver.cpp:79:21:   required from here
LinearProbing.h:82:4: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
In file included from /opt/local/include/gcc47/c++/iostream:40:0,
                 from Driver.cpp:1:
/opt/local/include/gcc47/c++/ostream:600:5: error:   initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = HashTable<Symbol>::HashEntry]'

Here is where I'm trying to print the vector in the HashTable class...

class HashTable
{
    ///...
    void print()
    {
        typename vector<HashEntry>::iterator vIter = array.begin;
        while(vIter != array.end())
        {
            cout<< *vIter << "\n";
            ++vIter;
        }
    }

    private:
        vector<HashEntry> array;
};

And my overloading in the Symbol class...

friend ostream & operator <<(ostream & outstream, Symbol & symbol) //overloaded to print out the the HashTable
{
    int num = symbol.type;
    string name = symbol.data;
    outstream << name << " : " << num << "\n";
    return outstream;
}
Foi útil?

Solução

Not sure what you're doing there but you have defined a vector of HashEntry and calling cout on HashEntry but your operator<< overload operates on Symbol instead.

The operator<< for class Symbol should look something like this

class Symbol {
    private:
        friend ostream& operator<<(ostream &os, const Symbol &s);

        int type;
        string data;


};

ostream& operator<<(ostream &os, const Symbol &s)
{
    os << s.data << " : " << s.type;
    return os;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top