tags:

views:

64

answers:

2

Hello!

I've just played with qjson library and got "underfinded reference" error. This is a 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, compilier says that it's "undefined reference to Person::Person" and all other functions in Person class. Why?

P.S. I'm newbie in c++ :)

+4  A: 

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?

Konrad Rudolph
A: 

You need to link with the library or object file that implements class Person.

If you have a libqjson.a file on a Unix variant, you need to add -lqjson to your link command line. If you're on Windows with qjson.lib, you need to link with qjson.lib. If you have a .cpp file that implements Person, you need to compile it and link it with your executable.

janm
I dit it, thanks.
dancingrobot84
Great! Stackoverflow works on voting and accepting ...
janm