Search code examples
c++qtqjson

Undefined reference to a local class


I've just played with qjson library and got "undefined reference" error. This is the code:

#include <qjson/qobjecthelper.h>
#include <qjson/serializer.h>

class Person: public QObject {

    Q_OBJECT

    Q_PROPERTY(QString name READ name WRITE setName)
    Q_PROPERTY(Gender gender READ gender WRITE setGender)
    Q_ENUMS(Gender)

public:
    Person(QObject *parent = 0);
    ~Person();

    QString name() const;
    void setName(const QString &name);

    enum Gender{Male, Female};
    Gender gender() const;
    void setGender(Gender gender);

private:
    QString m_name;
    Gender m_gender;

};

int main ()
{

    Person person;

    QJson::Serializer serializer;

    person.setName("Nick");
    person.setGender(Person::Male);
    QVariantMap person_map = QJson::QObjectHelper::qobject2qvariant(&person);

    QByteArray json = serializer.serialize(person_map);
    return 0;
}

So, compiler says that undefined reference to Person::Person and all other functions in Person class. Why?


Solution

  • You have only declared the methods of the class. You also need to define (i.e. implement) them. At the moment, how should the compiler know the constructor of Person is supposed to do?