[Qt-interest] Static members and QHash

Tero Mäntyvaara termant at gmail.com
Sun May 30 07:57:08 CEST 2010


I am using Qt Creator 1.2.1 "Based on Qt 4.5.2 (32 bit)" on 32-bit
Windows XP Pro.

What's wrong with my code? I get build error:

collect2: ld returned 1 exit status

Compile output:  

debug/classc.o: In function
`ZN5QHashI7QString6ClassAE8concreteEPN9QHashData4NodeE':
c:/Qt/2009.03/qt/include/QtCore/../../src/corelib/tools/qhash.h:(.text$_ZNK5QHashI7QString6ClassAE5valueERKS0_[QHash<QString,
ClassA>::value(QString const&) const]+0x57): undefined reference to
`ClassA::ClassA(ClassA const&)'
c:/Qt/2009.03/qt/include/QtCore/../../src/corelib/tools/qhash.h:(.text$_ZN9QHashNodeI7QString6ClassAEC1ERKS0_RKS1_[QHashNode<QString,
ClassA>::QHashNode(QString const&, ClassA const&)]+0x69): undefined
reference to `ClassA::ClassA(ClassA const&)'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\QClassTesting.exe] Error 1
mingw32-make: *** [debug] Error 2
Exited with code 2.
Error while building project QClassTesting
When executing build step 'Make'



I have figured it out that it has something to do with row 26 at file
classc.cpp but I don't know how to solve this.

File classc.h:   

#ifndef CLASSC_H
#define CLASSC_H

#include <QObject>
#include <QString>

#include "classb.h"

class ClassC : public QObject
{
private:
    static QString                 error;
    static ClassB properties;
public:
    ClassC &operator=(const ClassC &other);

    static void set(QString definition);
    static void set(QString target, QString definition);
};

#endif // CLASSC_H



File classc.cpp:

#include "classc.h"

#include <QRegExp>
#include <QObject>

QString ClassC::error;
ClassB ClassC::properties;

ClassC &ClassC::operator=(const ClassC &other) {
    return *this;
}

void ClassC::set(QString definition) {
    if(definition.contains(QRegExp("^ExtrenalA"))) {
        QRegExp rxlen("^ExtrenalA(?:\\(attribute=\"(\\w+)\"\\))?$");
        int pos = rxlen.indexIn(definition);
        if (pos > -1) {
            ClassC::properties.externalb.setVariable1(rxlen.cap(1));
        }
    }
}

void ClassC::set(QString target, QString definition) {
    if(ClassC::properties.classas.contains(target) == false) {
        ClassA classa = ClassA();
        ClassC::properties.classas.insert(target, classa); // What's the
problem? :-O
    }

    if(definition.contains(QRegExp("^ExtrenalB"))) {
        QRegExp rxlen("^ExtrenalB(?:\\(attribute=\"(\\w+)\"\\))?$");
        int pos = rxlen.indexIn(definition);
        if (pos > -1) {
           
ClassC::properties.classas.value(target).externala.setVariable1(rxlen.cap(1));
        }
    }
}



File classb.h:

#ifndef CLASSB_H
#define CLASSB_H

#include "types.h"
#include "classa.h"

#include <QString>
#include <QHash>

class ClassB
{
public:
    static types::ExtrenalB externalb;

    static QHash<QString, ClassA> classas;

    ClassB();
    ClassB(const ClassB &other);

    ClassB &operator=(const ClassB &other);

    bool operator==(const ClassB& other) const;
};

#endif // CLASSB_H



File classb.cpp:  

#include "classb.h"

types::ExtrenalB ClassB::externalb;
QHash<QString, ClassA> ClassB::classas;

ClassB::ClassB()
{
}

ClassB &ClassB::operator=(const ClassB &other) {
    return *this;
}

bool ClassB::operator==(const ClassB& other) const
{
  return true;
}



File classa.h: 

#ifndef CLASSA_H
#define CLASSA_H

#include "types.h"

class ClassA
{
public:
    static types::ExtrenalA externala;

    ClassA();
    ClassA(const ClassA &other);

    ClassA &operator=(const ClassA &other);

    bool operator==(const ClassA& other) const;
};

#endif // CLASSA_H



File classa.cpp:   

#include "classa.h"

types::ExtrenalA ClassA::externala;

ClassA::ClassA()
{
}

ClassA &ClassA::operator=(const ClassA &other) {
    return *this;
}

bool ClassA::operator==(const ClassA& other) const
{
  return true;
}



File types.h: 

#ifndef TYPES_H
#define TYPES_H

#include <QString>

namespace types {

    class Base
    {
    };

    class ExtrenalA: public Base
    {
        QString variable1;

    public:
        void setVariable1(QString variable1);
        QString getVariable1();

        bool operator==(const ExtrenalA& other) const;
    };

    class ExtrenalB: public Base
    {
        QString variable1;

    public:
        void setVariable1(QString variable1);
        QString getVariable1();

        bool operator==(const ExtrenalB& other) const;
    };

}

#endif // TYPES_H



File types.cpp:

#include "types.h"

namespace types {

    void ExtrenalA::setVariable1(QString newVariable1) {
        variable1 = newVariable1;
    }

    QString ExtrenalA::getVariable1() {
        return variable1;
    }

    bool ExtrenalA::operator==(const ExtrenalA& other) const {
      return variable1 == other.variable1;
    }

    void ExtrenalB::setVariable1(QString newVariable1) {
        variable1 = newVariable1;
    }

    QString ExtrenalB::getVariable1() {
        return variable1;
    }

    bool ExtrenalB::operator==(const ExtrenalB& other) const {
      return variable1 == other.variable1;
    }

}



File QClassTesting.pro:   

# -------------------------------------------------
# Project created by QtCreator 2010-05-29T09:37:59
# -------------------------------------------------
QT -= gui
TARGET = QClassTesting
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
    types.cpp \
    classa.cpp \
    classb.cpp \
    classc.cpp
HEADERS += types.h \
    classa.h \
    classb.h \
    classc.h



More information about the Qt-interest-old mailing list