[Qt-interest] Accessing a class member from an operator

Oliver Heins heins at sopos.org
Thu Apr 22 19:09:09 CEST 2010


In an application I have to restore pointers from saved data.
Therefore, I have to first read the whole data in and build the pointers
after the reading process has finished.  I thought I build a QHash<int,
int> to map unique keys.

QDataStream &operator>>(QDataStream &in, Token &token)
{
    quint32 partner;
    qint16 type;

    token.partner = 0;
    in >> token.unique >> partner;

    partnerMap.insert(token.unique, partner);

    return in;
}

partnerMap is declared in the class Editor, and the operator is declared
a friend function:

class Editor : public QWidget
{
    Q_OBJECT

public:
    Editor(QWidget *parent = 0);

    [...]

    friend QDataStream &operator<<(QDataStream &out, const Token &token);
    friend QDataStream &operator>>(QDataStream &in, Token &token);

    [...]

private:
    QHash<int, int> partnerMap;
    [...]
};

When compiling, I get an »error: ‘partnerMap’ was not declared in this
scope«, even if the operator is a friend of Editor.  I don't want to
make partnerMap global or static, which feels wrong to me even if I
don't think there could be concurrent access to it.

What would be a better design?

TIA, 
 olli



More information about the Qt-interest-old mailing list