I’ve some problem with using templates:
myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H
class myclass
{
private:
public:
~myclass();
myclass();
template<class S>
void login(S login, S pass);
};
#endif // MYCLASS_H
myclass.cpp
#include "myclass.h"
#include "../additional_func.h" // for connect(QString,QString) function
myclass::myclass()
{
}
template <typename S>
void myclass::login(S login, S pass)
{
additional_func->connect(login,pass);
}
myclass::~myclass()
{
}
in mainwindow.cpp (i’m using QT)
myclass *vr = new vr();
vr->login(ui->linelogin->text(),ui->linepwd->text()); // QString, QString
And I get error:
mainwindow.cpp:31: error: undefined reference to `void ecore::connect(QString, QString)’
What way for using myclass::connect in other classes?
You’ve declared
login()withinmyclassin your header, but you don’t show us here that you’ve defined it anywhere.Putting a semicolon after the function signature simply declares that this function exists. It doesn’t create a function. You have to define the function in order to use it.
You can define it within your header, by putting the function body in
{}instead of a semicolon.Placing the definition here is one way to indicate to the compiler that you wish to inline the function, although the compiler may choose not to, anyway.
Or, you could define it within your .cpp file just like the other functions you’ve defined.Because this is a template function that will (presumably) be shared by multiple code files, it has to be defined within the header.
Without one of these, there is no function body defined for the compiler to execute when you call this function, hence the “undefined reference” error.