Good day all
Please note: C++ newbie here
I am creating shared libraries among various other c++ features to allow for a complete understanding, however I am at a loss.
Problem:
As the title suggests, a list of errors:
I have no idea what causes them, and googling does not provide much insight either. As suggested here to add the Q_Object macro, I have done so but obviously it is of no use.
Various other SO posts suggest checking the correct header, etc which is correct.
Error:
netm.cpp:3: error: undefined reference to `vtable for netm'
netm.cpp:3: error: undefined reference to `_imp___ZN4miscC1Ev'
netm.cpp:6: error: undefined reference to `vtable for netm'
netm.cpp:6: error: undefined reference to `_imp___ZN4miscC1Ev'
//...
I have several more errors similar to these above, but solving these should assist me in resolving the rest
From all the tutorials, etc I have followed, I have done nothing out of the ordinary.
Note: I am unsure what information is all required, if more is required, I'll gladly share.
//.pro
QT -= gui
QT += network
TARGET = netm
TEMPLATE = lib
DEFINES += NETM_LIBRARY
SOURCES += netm.cpp
HEADERS += netm.h\
netm_global.h
unix {
target.path = /usr/lib
INSTALLS += target
}
//netm_global.h - FULL
#ifndef NETM_GLOBAL_H
#define NETM_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(NETM_LIBRARY)
# define NETMSHARED_EXPORT Q_DECL_EXPORT
#else
# define NETMSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // NETM_GLOBAL_H
//netm.h - FULL
#ifndef NETM_H
#define NETM_H
#include "netm_global.h"
#include "../misc/misc.h"
#include "../gen/gen.h"
#include <QHostInfo>
#include <QTcpSocket>
class NETMSHARED_EXPORT netm
{
Q_OBJECT
public:
netm();
netm(QString hostname);
bool Send(int portNumber, char* message = NULL);
ReturnObject read();
bool isServerOnline(QString IP = QString());
int getPing(QString IP = QString());
void getIP();
void disconnectFromServer();
~netm();
private slots:
void getIP();
private:
misc m;
QHostInfo serverInfo;
QHostAddress serverIP;
QTcpSocket tcp_con;
};
#endif // NETM_H
//netm.cpp - Partial
#include "netm.h"
netm::netm(){ <--- ERROR line
}
netm::netm(QString hostname) <--- ERROR line
{
serverInfo.lookupHost(hostname, 0, SLOT(getIP()));
}
//...
Help (with explanations) would be greatly appreciated!
UPDATE
As suggested, I defined the constructor in misc.cpp, since it was not present.
Recompiling, I read an error mentioning that the netm class needed to inherit from QObject.
Thus adding/changing:
//netm.h - Partial
#include //...
#include <QObject>
class NETMSHARED_EXPORT netm : public QObject
{
Q_OBJECT
public:
netm();
netm(QString hostname);
//...
};
Errors persist:
netm.cpp:3: error: undefined reference to `_imp___ZN4miscC1Ev'
netm.cpp:3: error: undefined reference to `_imp___ZN4miscD1Ev'
netm.cpp:6: error: undefined reference to `_imp___ZN4miscC1Ev'
netm.cpp:6: error: undefined reference to `_imp___ZN4miscD1Ev'
For completeness sake (misc is also a dynamic library):
//misc.h
#ifndef MISC_H
#define MISC_H
#include "misc_global.h"
#include <QString>
#include <QList>
class MISCSHARED_EXPORT misc
{
public:
misc();
~misc();
//String Literals
//Network related
static QString googleDNS;
//Command Codes
static QString CMD_AUTH;
static QString CMD_REQ;
//Request Codes
static QString REQ_USER_INFO;
static QString REQ_VPN_DATA;
static QString REQ_VPN_UP;
//...
};
//misc.cpp
#include "misc.h"
misc::misc(){
//Network related
QString googleDNS = QStringLiteral("8.8.8.8");
//Command Codes
QString CMD_AUTH = QStringLiteral("AUTH");
QString CMD_REQ = QStringLiteral("REQ");
//Request Codes
QString REQ_USER_INFO = QStringLiteral("USER_INFO");
QString REQ_VPN_DATA = QStringLiteral("VPN_DATA");
QString REQ_VPN_UP = QStringLiteral("VPN_UP");
}
misc::~misc(){}
As seen here, the constructor exists,
any other thoughts?
Missing calls to _imp___ZN4miscC1Ev, which is misc::misc() according to c++filt, likely means that the class misc is missing a defined default constructor. Check to make sure you're compiling in a definition for misc::misc().
For the vtable error, make sure that you've provided a definition (even if empty or stubbed out) for every function declared in netm (or at minimum all of the virtual functions in netm). The vtable for a class references every virtual function, so all of the virtual functions must be defined or it will not compile.
Related
This question is similar to one that has been asked before, but I am having an issue with Qt recognizing my class and functions despite them being defined. I am doing this in linux where the Qt version is only 5.5 while the windows version is at 5.11. When running the same program in windows, I get no errors.
I am getting "undefined reference to QCanBus::instance()" errors
This is happening for every QCanBus function. In my code snippet from connectdialogue.cpp, only the first occurrence is featured and can be found at the second to last code line and gives the errors:
"undefined reference to QCanBus::instance()"
"undefined reference to QCanBus::plugins()"
These errors are given despite being defined in the qcanbus.h headder file.
I have tried adding the lines static QCanBus *instance() or alternatively QCanBus *instance() under the #include lines in my connectdialogue.cpp, but I am then presented with the warning:
"QCanBus::instance() is defined, but unused" when it is clearly used on the second to last line of the featured code snippet.
How do I fix these errors?
connectdialogue.cpp
#include "connectdialog.h"
#include "ui_connectdialog.h"
#include "qcanbus.h"
ConnectDialog::ConnectDialog(QWidget *parent) :
QDialog(parent),
m_ui(new Ui::ConnectDialog)
{
m_ui->setupUi(this);
m_ui->errorFilterEdit->setValidator(new QIntValidator(0, 0x1FFFFFFFU, this));
m_ui->loopbackBox->addItem(tr("unspecified"), QVariant());
m_ui->loopbackBox->addItem(tr("false"), QVariant(false));
m_ui->loopbackBox->addItem(tr("true"), QVariant(true));
m_ui->receiveOwnBox->addItem(tr("unspecified"), QVariant());
m_ui->receiveOwnBox->addItem(tr("false"), QVariant(false));
m_ui->receiveOwnBox->addItem(tr("true"), QVariant(true));
m_ui->canFdBox->addItem(tr("false"), QVariant(false));
m_ui->canFdBox->addItem(tr("true"), QVariant(true));
m_ui->dataBitrateBox->setFlexibleDateRateEnabled(true);
connect(m_ui->okButton, &QPushButton::clicked, this, &ConnectDialog::ok);
connect(m_ui->cancelButton, &QPushButton::clicked, this, &ConnectDialog::cancel);
connect(m_ui->useConfigurationBox, &QCheckBox::clicked,
m_ui->configurationBox, &QGroupBox::setEnabled);
connect(m_ui->backendListBox, &QComboBox::currentTextChanged,
this, &ConnectDialog::backendChanged);
connect(m_ui->interfaceListBox, &QComboBox::currentTextChanged,
this, &ConnectDialog::interfaceChanged);
m_ui->rawFilterEdit->hide();
m_ui->rawFilterLabel->hide();
m_ui->backendListBox->addItems(QCanBus::instance()->plugins());
updateSettings();
}
qcanbus.h
#ifndef QCANBUS_H
#define QCANBUS_H
#include <QtCore/qobject.h>
#include "qserialbusglobal.h"
#include "qcanbusdevice.h"
#include "qcanbusdeviceinfo.h"
QT_BEGIN_NAMESPACE
class Q_SERIALBUS_EXPORT QCanBus : public QObject
{
Q_OBJECT
public:
static QCanBus *instance();
QStringList plugins() const;
QList<QCanBusDeviceInfo> availableDevices(const QString &plugin, QString *errorMessage = nullptr) const;
QCanBusDevice *createDevice(const QString &plugin,
const QString &interfaceName,
QString *errorMessage = nullptr) const;
private:
QCanBus(QObject *parent = nullptr);
Q_DISABLE_COPY(QCanBus)
};
QT_END_NAMESPACE
#endif // QSERIALBUS_H
QCanBus was missing QCanBus.cpp and was not declared.
In my code, I have a file VisualForm.h/.cpp that needs access to a private member in MainWindow.h/.cpp.
My MainWindow.h includes the following (relevant) files:
#include "connLines.h"
#include "nodeShapes.h"
#include "VisualForm.h"
My MainWindow.h contains a class, TInfluenceDiagram, which has public members as listed:
class TInfluenceDiagram : public TForm
{
public: // members
connLine genericLine;
int tempSaveX, tempSaveY;
}
MainWindow.cpp declares a pointer to the window that is created:
TInfluenceDiagram *InfluenceDiagram;
connLine is a class that I have defined in the connLines.h/.cpp files.
My VisualForm.h includes the following relevant files in this order:
#include "connLines.h"
#include "MainWindow.h"
And contains this struct:
struct connProperties{
connLine conn;
TPoint linepoints[2];
}connProperties;
In my VisualForm.cpp file is where I am getting my error. This file needs access to MainWindow's genericLine public member but my compiler is telling me that it is not a member of TInfluenceDiagram.
Here is the function that produces the error:
void __fastcall TVisualPropertiesForm::Button1Click(TObject *Sender)
{
InfluenceDiagram->tempSaveX = 0; //This public member is accessed just fine
InfluenceDiagram->genericLine = connProperties.conn;; //Error here
}
The exact error I get in C++ Builder XE8 is:
"[bcc32 Error] VisualForm.cpp(331): E2316 'genericLine' is not a member of 'TInfluenceDiagram'"
This stackoverflow question is similar: Compiler saying variable is not a member of class when it is
But I have tried changing the order of the includes as suggested in the top answer but to no avail so I have posted this as a new question. Any help would be greatly appreciated.
Here's my code:
menuState.hpp
#ifndef MENU_STATE_HPP
#define MENU_STATE_HPP
#include "state.hpp"
#include <SFML/Graphics.hpp>
class MenuState : public State
{
public:
MenuState();
static void create(StateListener* Parent, const std::string name)
{
MenuState* myState = new MenuState();
myState->parent = Parent;
Parent->manageState(name, myState);
}
void enter();
void exit();
void resume();
private:
};
#endif // MENU_STATE_HPP
I'm getting an undefined reference to the constructor when I do MenuState* myState = new MenuState(); and I'm not sure why because MenuState::MenuState() comes before the create function in the class declaration.
EDIT: I'm also getting the same error to all my sfml functions.
Here's the exact build messages: http://pastebin.com/e819FhPj
I do have the sfml libraries linked and the header path set in my compilers search directories.
It is always best to show the exact text of the error, but my educated guess is, you are getting a linker error, not a compiler error. Have you actually implemented the constructor anywhere? I bet you haven't.
I'm not an experience C++ developer but it seems like MenuState constructor is declared but not defined.
Replace MenuState(); with MenuState(){} should fix the error.
I have some functions in delphi dll, and I want to load them (using QtLibrary) at once.
Can I store that functions in global variables to use it? I tried to declare global function pointer in .h file and resolve them in main file, but got error "multiple definition"
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qlibrary.h>
#include <QDebug>
#include "mapwidget.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString path = "map_dll.dll";
if (QLibrary::isLibrary(path)) {
lib = new QLibrary(path);
lib->load();
if (!lib->isLoaded()) {
qDebug() << lib->errorString();
} else {
nearestWell = (void( *)(double x,
double y,
double &wellX,
double &wellY))
lib->resolve("nearestWell");
}
}
}
void MainWindow::on_pushButton_2_clicked()
{
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLibrary>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QLibrary *lib;
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mapwidget.h
#ifndef MAPWIDGET_H
#define MAPWIDGET_H
#include <QWidget>
#include <QPainter>
typedef void (*NearestWellFunc) (double x, double y, double &wellX,
double &wellY);
extern NearestWellFunc nearestWell;
....
#endif // MAPWIDGET_H
error message:
debug/mainwindow.o: In function `ZN10MainWindow21on_pushButton_clickedEv':
C:\nipi\APT\map_qt\build-MIG-Desktop_Qt_5_1_0_MinGW_32bit-Debug/../MIG/mainwindow.cpp:33: undefined reference to `nearestWell'
C:\nipi\APT\map_qt\build-MIG-Desktop_Qt_5_1_0_MinGW_32bit-Debug/../MIG/mainwindow.cpp:40: undefined reference to `nearestWell'
C:\nipi\APT\map_qt\build-MIG-Desktop_Qt_5_1_0_MinGW_32bit-Debug/../MIG/mainwindow.cpp:54: undefined reference to `nearestWell'
Makefile.Debug:84: recipe for target 'debug/MIG.exe' failed
mingw32-make[1]: Leaving directory 'C:/nipi/APT/map_qt/build- MIG-Desktop_Qt_5_1_0_MinGW_32bit-Debug'
makefile:34: recipe for target 'debug' failed
C:\nipi\APT\map_qt\build-MIG-Desktop_Qt_5_1_0_MinGW_32bit-Debug/../MIG/mainwindow.cpp:63: undefined reference to `nearestWell'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [debug/MIG.exe] Error 1
mingw32-make: *** [debug] Error 2
This error:
undefined reference to `nearestWell'
Is the compiler saying "I know that there is a thing called nearestWell, but I don't know where it is stored (because it hasn't been defined)"
This line:
extern NearestWellFunc nearestWell;
Says to the compiler "somewhere else there will be a NearestWellFunc called nearestWell".
This is a declaration - it is telling the compiler that there will be a variable called nearestWell somewhere later.
It needs to be paired with a definition that tells the compiler to set aside some space for the variable. In this case, it would look something like:
NearestWellFunc nearestWell = /* whatever the initial value should be */
Remember, you can only define things once, or the compiler will get confused. This is why you need to put the declaration inside a .cpp file rather than the .h file - if you put the definition in the header file, each .cpp file that includes that header will include the definition, which is what is causing the multiple definition error.
Looking at your example structure, I would put the definition in mainwindow.cpp.
You can declare the variables in a header file, but you can't define them.
For example, declaring a variable as extern in a header file is fine. You probably define them instead. Add the extern keyword to your declarations in the header file, and add definitions in a source file.
I'm a Java developer experimenting with C++.
I just created a new class. In my other class I want to have list where I can store Filter objects.
Filter.h
#ifndef FILTER_H_
#define FILTER_H_
class Filter {
public:
Filter(int id);
int id;
~Filter();
};
#endif /* FILTER_H_ */
Filter.cpp
#include "Filter.h"
Filter::Filter(int id) {
this.id = id;
}
Filter::~Filter() {
}
Cars.h
#include "Filter.h"
...
...
private:
std::vector<Filter> filters;
Cars.cpp
so in a function here I try to do this:
int id = 2;
Filter *filter = new Filter(id);
which generate this error:
Cars.cpp:120: undefined reference to `Filter::Filter(int)'
stl_construct.h:83: undefined reference to `Filter::~Filter()'
What's the reason for this?
The error is generated by the linker because it can not see where the definition of the constructor is located.
If you are using an IDE, you should add both .cpp files to the project so that they can be compiled together and the definition would be found by the linker.
If not, then you have to combine them yourself -assuming you are using gcc:
g++ cars.cpp filter.cpp
will combine them into one executable and should not show you that error