试图打印超载对象的矢量?

时间:2014-03-19 12:28:56

标签: c++ vector iterator hashtable overloading

我正在研究涉及线性探测的哈希表程序。我试图打印出Symbol类对象的向量,但每次尝试时都会遇到两个错误。我在下面发布了以下错误:

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]'

这是我尝试在HashTable类中打印矢量的地方......

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

    private:
        vector<HashEntry> array;
};

我在Symbol班级中超载......

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

1 个答案:

答案 0 :(得分:0)

不确定你在那里做了什么,但你已经定义了一个HashEntry的向量并在HashEntry上调用cout但你的运算符&lt;&lt;而不是在符号上运行。

运营商&lt;&lt;对于类,符号应该看起来像这样

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