简体   繁体   English

尝试打印带有重载的对象矢量?

[英]Trying to print vector of objects with overloading?

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. 我正在尝试打印出Symbol类对象的向量,但是每次尝试这样做都会遇到两个错误。 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... 这是我试图在HashTable类中打印矢量的地方...

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... 还有我在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;
}

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. 不确定您在那做什么,但是您已经定义了HashEntry的向量并在HashEntry上调用cout,但是您的operator <<重载代替了对Symbol的操作。

The operator<< for class Symbol should look something like this 类Symbol的operator <<应该看起来像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM