I'm having strange linkage problems with the following very simple application, which has a class inheriting both QObject and an interface class.
#include <QApplication>
#include <memory>
#include <iostream>
#include <qobject>
class IFoo {
public:
virtual ~IFoo() {}
virtual void foo()=0;
};
class Foo: public QObject, public IFoo
{
Q_OBJECT
public:
explicit Foo(QObject *parent=0): QObject(parent) { std::cout << "foo ctor" << std::endl; }
void foo() override { std::cout << "foo::foo" << std::endl; }
};
std::unique_ptr<IFoo> createFoo() { return std::unique_ptr<IFoo>(new Foo()); }
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
auto foo = createFoo();
return a.exec();
}
This causes the following errors:
LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Foo::metaObject(void)const " (?metaObject#Foo##UEBAPEBUQMetaObject##XZ)
LNK2001: unresolved external symbol "public: virtual void * __cdecl Foo::qt_metacast(char const *)" (?qt_metacast#Foo##UEAAPEAXPEBD#Z)
LNK2001: unresolved external symbol "public: virtual int __cdecl Foo::qt_metacall(enum QMetaObject::Call,int,void * *)" qt_metacall#Foo##UEAAHW4Call#QMetaObject##HPEAPEAX#Z)
LNK1120: 3 unresolved externals
I have obviously tried run qmake, clean and rebuild without effect. If I make the class Foo a plain C++ class by removing all Qt references, it works correctly.
What could be wrong?
Related
I am having problems using the QObject. In my code I attempt to add a object to Javascript in Qt5.
#include <QtGui>
#include <QtWebKit>
#include <QApplication>
#include <QWebView>
#include <QWebFrame>
#include <QWebPage>
#include <iostream>
#include <string>
using namespace std;
class nativeObject : public QObject {
Q_OBJECT
public:
string version;
};
int main(int argc, char** argv) {
nativeObject test;
test.version = "BETA";
QApplication app(argc, argv);
QWebView view;
QWebFrame *frame = view.page()->mainFrame();
frame->addToJavaScriptWindowObject("someNameForMyObject", &test);
view.setUrl(QUrl("http://google.com"));
view.show();
return app.exec();
}
Running the code gives the following errors:
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall nativeObject::metaObject(void)const " (?metaObject#nativeObject##UBEPBUQMetaObject##XZ)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __thiscall nativeObject::qt_metacast(char const *)" (?qt_metacast#nativeObject##UAEPAXPBD#Z)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __thiscall nativeObject::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#nativeObject##UAEHW4Call#QMetaObject##HPAPAX#Z)
release\webkit.exe:-1: error: LNK1120: 3 unresolved externals
I cannot find any "good" and relevant documentation as I am very new in programming in qt and C++. Have I declared the QObject incorrectly or is there something else I am doing wrong?
Try this:
frame->addToJavaScriptWindowObject("someNameForMyObject", &test);
It requires QObject* but you set QObject.
void QWebFrame::addToJavaScriptWindowObject(const QString & name, QObject * object, ValueOwnership own = QtOwnership)
& used here to get address of object because it is not a pointer, you can also create nativeObject as pointer:
nativeObject *test = new nativeObject;
In this case
frame->addToJavaScriptWindowObject("someNameForMyObject", test);
will be valid because test is already pointer. Note that for pointers we use -> instead of .
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
I have a problem with the Linker which I just can't solve..
Already tried anything I could think of
I have a Baseclass (Person) and a Derived Class (Dealer) and I just want to call the Constructor from the CardStack Class which is a member in the Dealer class.
Here is my code:
Person.h
#ifndef PERSON_H
#define PERSON_H
#include "Card.h"
#include "Hand.h"
class Person
{
public:
Person(void);
virtual ~Person(void);
virtual bool TakeCard(Card c);
virtual bool Lost(void);
protected:
virtual void CheckLost(void);
bool b_Lost;
Hand m_Hand;
};
#endif
Dealer.h
#ifndef DEALER_H
#define DEALER_H
#include "Person.h"
#include "Card.h"
#include "CardStack.h"
class Dealer : public Person
{
public:
Dealer(int stackcount);
virtual ~Dealer(void);
bool TakeCard(Card c);
bool Lost(void);
Card GiveCard(Card c);
protected:
void CheckLost(void);
CardStack m_GameStack;
};
#endif
Dealer.cpp
#include "Dealer.h"
Dealer::Dealer(int stackcount) : Person(), m_GameStack(stackcount)
{
};
Dealer::~Dealer(void)
{
};
bool Dealer::TakeCard(Card c)
{
if(!b_Lost || m_Hand.GetTotal() <= 17)
{
m_Hand.Take(c);
CheckLost();
return true;
}
return false;
};
void Dealer::CheckLost()
{
if (m_Hand.GetTotal() > 21)
{
b_Lost = true;
}
};
bool Dealer::Lost()
{
return b_Lost;
};
I honestly tried everthing I could think of but I couldn't figure out what the mistake is...
Here is the Output when compiling Dealer.cpp:
1>Dealer.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Person::~Person(void)" (??1Person##UAE#XZ) referenced in function __unwindfunclet$??0Dealer##QAE#H#Z$0
1>Dealer.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall Person::TakeCard(class Card)" (?TakeCard#Person##UAE_NVCard###Z)
1>Dealer.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall Person::Lost(void)" (?Lost#Person##UAE_NXZ)
1>Dealer.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall Person::CheckLost(void)" (?CheckLost#Person##MAEXXZ)
It looks like you are trying to compile Dealer.cpp into a program on its own. That doesn't work because it depends on the definition of the methods in Person, which are probably in Person.cpp. It would have been helpful if you had shown us the command you used to compile. But assuming you're using g++, what you probably tried to do is
g++ Dealer.cpp
What you should have done is either
g++ Person.cpp Dealer.cpp etc.
or
g++ -c Dealer.cpp
g++ -c Person.cpp
etc., and then
g++ Dealer.o Person.o etc.
I'm creating a QLabel subclass which adds the DoubleClickEvent to it. I have created the following, but I'm getting some strange linker errors, maybe someone can point out what I've done wrong?
//Header
#ifndef IMAGE_LABEL_H
#define IMAGE_LABEL_H
#include <QLabel>
#include <QMouseEvent>
class image_label : public QLabel
{
Q_OBJECT
public:
image_label(QWidget* parent = 0);
~image_label();
signals:
void doubleClicked();
protected:
void mouseDoubleClickEvent(QMouseEvent * e);
};
#endif
//CPP
#include "image_label.h"
#include <QMouseEvent>
image_label::image_label(QWidget* parent) : QLabel(parent)
{
}
image_label::~image_label()
{
}
void image_label::mouseDoubleClickEvent(QMouseEvent* e)
{
if (e->button() == Qt::LeftButton)
{
emit doubleClicked();
QLabel::mouseDoubleClickEvent(e);
}
}
I get the following linker errors when I compile:
image_label.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall image_label::metaObject(void)const " (?metaObject#image_label##UBEPBUQMetaObject##XZ)
image_label.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall image_label::qt_metacast(char const *)" (?qt_metacast#image_label##UAEPAXPBD#Z)
image_label.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall image_label::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#image_label##UAEHW4Call#QMetaObject##HPAPAX#Z)
image_label.obj : error LNK2019: unresolved external symbol "protected: void __thiscall image_label::doubleClicked(void)" (?doubleClicked#image_label##IAEXXZ) referenced in function "protected: virtual void __thiscall image_label::mouseDoubleClickEvent(class QMouseEvent *)" (?mouseDoubleClickEvent#image_label##MAEXPAVQMouseEvent###Z)
Can anybody help why I get these errors?
You must run the MOC preprocessor on the file image_label.h. This generates a file moc_image_label.cppthat you must include in the build. The error message indicates that you have not done this. (The symbols image_label::metaObject etc. that are mentioned in the error message are defined in moc_image_label.cpp.)
#include <iostream>
using namespace std;
class A {
public:
void function( int num);
bool function1()const;
virtual bool function2() const=0;
};
class B:public A {
public :
bool function2()const;
};
int _tmain(int argc, _TCHAR* argv[])
{
void (A::* p)(int)= &A::function; //不是地址,而是一个指向成员函数的指针
// Edit: Google translation of the above comment is
// "Not address, but a pointer to a member function pointer"
bool (A::* p1)()const =&A::function1; // 指向成员函数的指针可以指向一个常量成员函数
// Edit: Google translation of the above comment is
// "Point to a member function pointer can point to a const member function"
B b;
A *a=&b;
(a->*p1)();
(b.*p1)();
return 0;
}
but when I link it:
1>c.obj : error LNK2019: unresolved external symbol "public: bool __thiscall A::function1(void)const " (?function1#A##QBE_NXZ) referenced in function _wmain
1>c.obj : error LNK2019: unresolved external symbol "public: void __thiscall A::function(int)" (?function#A##QAEXH#Z) referenced in function _wmain
1>c.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall B::function2(void)const " (?function2#B##UBE_NXZ)
can you tell me why?
You haven't implemented A::function(), A::function1(), or B::function2(). You need to do that.
A::function1, A::function and B::function2 are all declared, but never defined. You can't get a pointer to the function if it is not defined, where would it point?
Here the code
#include <iostream>
#include <conio.h>
using namespace std;
template <typename T> class grid
{
public:
grid();
~grid();
void createCells();
private:
T **cells;
};
int main(int argc, char **argv)
{
grid<int> intGrid;
_getch();
return 0;
}
While trying to compile - got a message:
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall
grid<int>::~grid<int>(void)" (??1?$grid#H##QAE#XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall
grid<int>::grid<int>(void)" (??0?$grid#H##QAE#XZ) referenced in function _main
What need to do?
You need to define the constructor and destructor (you just declared them):
template <typename T> class grid
{
public:
grid()
{} // here
~grid()
{} // and here
void createCells();
private:
T **cells;
};