1

I am quite new to Qt framework. I try to code a class that contains a 2D array whose data is supposed to be displayed in the main window (using QTableWidget, for example). The class has the following declaration:

// gridgame.h
template <int gsize_r,int gsize_c>
class GridGame : public QObject
{
    Q_OBJECT
private:
    char field[gsize_r][gsize_c];
public:
    GridGame();
    void setupConnect(QMainWindow & win);

signals:
    void onPrintStatus(char[gsize_r][gsize_c]);
};

The definition of the class is of course given in cpp file as follows:

//gridgame.cpp

#include "gridgame.h"

template <int gsize_r,int gsize_c>
GridGame<gsize_r,gsize_c>::GridGame()
{
    int i,j;
    for (i=0;i<gsize_r;++i)
        for (j=0;j<gsize_c;++j)
            field[i][j] = '-';
}

template <int gsize_r,int gsize_c>
void GridGame<gsize_r,gsize_c>::setupConnect(QMainWindow &win)
{
    connect(this,SIGNAL(onPrintStatus()),&win,SLOT(onRefresh()));
}

template <int gsize_r,int gsize_c>
void GridGame<gsize_r,gsize_c>::onPrintStatus(char[gsize_r][gsize_c]){
    emit field;
}

The class is used in main as follows:

#include <QtCore>
#include <QApplication>
#include <QObject>
#include "mainwindow.h"
#include "gridgame.cpp"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    const int sizer = 5, sizec = 5;
    GridGame<sizer,sizec> gg;
    w.show();
    gg.printStatus();

    return a.exec();
}

Question: The code seems to compile ok, but I get the linker errors even if I do not call any function of the class (just have a declaration of a variable)

-1: error: undefined reference to `GridGame<5, 5>::metaObject() const'
-1: error: undefined reference to `GridGame<5, 5>::qt_metacast(char const*)'
-1: error: undefined reference to `GridGame<5, 5>::qt_metacall(QMetaObject::Call, int, void**)'
-1: error: error: ld returned 1 exit status

What might be the problem? I am using Qt Creator 3.6.1 and Qt library 5.6.0.

Thanks in advance for any help.

NeoSer
  • 527
  • 3
  • 16
  • 2
    You can't really have templated QObjects: http://stackoverflow.com/q/4397478/211160 – HostileFork says dont trust SE May 04 '16 at 18:41
  • How can I fix it then? – NeoSer May 04 '16 at 18:44
  • 2
    Up to you. Using templates to hardcode the size of a game board at compile time is possibly overkill for what you are doing--though I don't know what you're doing. It seems either you've got a lot of instances of these boards *(in which case, people would have to query instances for their size anyway, so why not just pass the size into the constructor and use vectors/lists)* or you have one and it's constant *(in which case, a constant is more convenient)*. – HostileFork says dont trust SE May 04 '16 at 18:53
  • Well, the idea is that a player specifies the size of the board at run-time (possibly through the main window). I could probably use vector to declare the board, but how can I then display it on the form (using signals and slots if possible)? – NeoSer May 04 '16 at 18:56
  • 2
    If the idea is for the player to specify the size of the board at run time, then you definitely don't want to be passing the size as template parameters...because those get baked-in at compile-time. :-/ [this article may help](http://blog.hostilefork.com/modern-cpp-or-modern-art/) with understanding how template parameters work. – HostileFork says dont trust SE May 04 '16 at 19:00

0 Answers0