I've been all over google trying to understand why I can't get this to work
I've tried all kinds of things I've seen out there, even including the source file (ugh!) but whenever I'm not getting compiler errors, I'm getting linker errors.
OK, maybe I'm just a rube.
Anyway the code below is throwing this compiler exception.
LNK2019 unresolved external symbol public: virtual __thiscall ControlGroup<class QRadioButton> .. yada ... yada ... yada
Here is the code that declares an instance of the template in a header file.
bits.h
#include "controlgroup.h"
class Ui_bits
{
public:
ControlGroup<QRadioButton> *pBitSize;
}
#include "controlgroup.h"
class Ui_bits
{
public:
ControlGroup<QRadioButton> *pBitSize;
}
To copy to clipboard, switch view to plain text mode
Here's the call that invokes the template and incurs the wrath of the linker.
bits.cpp
#include "bits.h"
void some_init_func()
{
Twidget tw;
ui->pBitSizes = new ControlGroup <QRadioButton>(&tw, ui->centralWidget);
}
#include "bits.h"
void some_init_func()
{
Twidget tw;
ui->pBitSizes = new ControlGroup <QRadioButton>(&tw, ui->centralWidget);
}
To copy to clipboard, switch view to plain text mode
Here is the header for the offending template class...
controlgroup.h
#ifndef CONTROLGROUP_H
#define CONTROLGROUP_H
#include <QtCore>
#include <QtGui>
typedef void (*pf)(int);
class Twidget
{
public:
int count;
};
template <typename T>
class ControlGroup
: public QWidget{
public:
explicit ControlGroup
(Twidget
*tw,
QWidget *parent
=0) {
init (tw, parent );
}
ControlGroup(){};
void init
( Twidget
*tw,
QWidget *parent
);
~ControlGroup();
QList<T> widget;
private:
Twidget *m_tw;
#include "controlgroup.cpp"
};
#endif // CONTROLGROUP_H
#ifndef CONTROLGROUP_H
#define CONTROLGROUP_H
#include <QtCore>
#include <QtGui>
typedef void (*pf)(int);
class Twidget
{
public:
QString name;
QSignalMapper mapper;
QList <pf> slotList;
int count;
};
template <typename T>
class ControlGroup : public QWidget
{
public:
explicit ControlGroup(Twidget *tw, QWidget *parent=0)
{
init (tw, parent );
}
ControlGroup(){};
void init( Twidget *tw, QWidget *parent);
~ControlGroup();
QList<T> widget;
private:
Twidget *m_tw;
QWidget *m_parent;
#include "controlgroup.cpp"
};
#endif // CONTROLGROUP_H
To copy to clipboard, switch view to plain text mode
... and here's the source ...
controlgroup.cpp
#ifndef CONTROLGROUP_CPP
#define CONTROLGROUP_CPP
#include "controlgroup.h"
template <typename T>
void init
( Twidget
*tw,
QWidget *parent
) {
for (int index = 0; index < tw->count; index++)
{
widget.append(new <T>(parent));
}
}
#endif
#ifndef CONTROLGROUP_CPP
#define CONTROLGROUP_CPP
#include "controlgroup.h"
template <typename T>
void init( Twidget *tw, QWidget *parent )
{
for (int index = 0; index < tw->count; index++)
{
widget.append(new <T>(parent));
}
}
#endif
To copy to clipboard, switch view to plain text mode
I'm sure it's something stupid, and that you guys frequently see ignorance like this.
It's probably a face-palmer, but I just can't figure it out.
Bookmarks