Is it possible to have a generic function declaration in a header file and the subsequent definition in a definition file?
I'd like to do something like this
HEADER FILE:
#ifndef PROCEDURES_H #define PROCEDURES_H template<class T> T max(const T&, const T&); #endif DEFINITION FILE:
#include "procedures.h" template<class T> T max(const T& left, const T& right) { return left > right ? left : right; } This results in a linker error.
Of course if I just put everything in the header it works fine.
maxis not a "generic function". It is a template. That error in perception may underpin the entirety of your confusion.#include-ing your definition file at the bottom of your header file? You could establish a convention like all your defintion files have extension.hppor.tpp. But really, you should read a little about template instantiation and why this doesn't work.procedures.cppI would#include "procedures.cpp"at the bottom of my header file?.cppextension. Please see the accepted answer here: stackoverflow.com/questions/3526299/…