对本地类的未定义引用
我刚刚玩过 qjson 库并收到“未定义的引用”错误。这是代码:
#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;
}
因此,编译器表示 对 Person::Person
以及 Person 类中的所有其他函数的未定义引用。为什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您仅声明该类的方法。您还需要定义(即实现)它们。目前,编译器应该如何知道
Person
的构造函数应该做什么?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?您需要链接实现类 Person 的库或目标文件。
如果您在 Unix 变体上有 libqjson.a 文件,则需要将 -lqjson 添加到链接命令行。如果您在 Windows 上使用 qjson.lib,则需要链接 qjson.lib。如果您有一个实现 Person 的 .cpp 文件,您需要编译它并将其与可执行文件链接。
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.