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*)'

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.