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?
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?
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.
精彩评论