0

Hello,

So I'm having trouble figuring out what the correct syntax is for a function with a return type that is a template type and is within a namespace. Anyone see anything wrong with the code below?

-------.HPP file:

#pragma once

#ifndef EC_UTIL_H
#define EC_UTIL_H

#include "../CommonHeaders.hpp"
#include "../core/common/APVertex.hpp"

#include <QString>

class QWidget;

namespace EditorCommon
{ 
   template <class T> T *findAndAssert(QString id, QWidget *root);
}

#endif



#include "ECUtil.hpp"

------------------- .CPP file:

#include <QApplication>
#include <QWidget>
#include <QMessageBox>

namespace EditorCommon
{
   template <class T> T *findAndAssert(QString id, QWidget *root)
   {
      T *w = root->findChild<T *>(id);

      if(!w)
      {
         QMessageBox::critical(root , QString("Critical Application Error"), QString("Missing UI element: ") + id);
         exit(EXIT_FAILURE);
      }

      return w;
   }
}

------- In use:

mColumn.mEnableSnap = (QCheckBox *) findAndAssert<QWidget>(QString("uiEnableSnap"), panel);

------ Error:
WorkspaceDelegate.cpp:(.text+0x141): undefined reference to `QWidget* EditorCommon::findAndAssert<QWidget>(QString, QWidget*)'

2
Contributors
1
Reply
30
Views
2 Years
Discussion Span
Last Post by sepp2k
0

The syntax is right, but for template functions the definition needs to be in the header file, not the .cpp file.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.