Hi++,
I get a link error when I try to call a template function that is
declared in a header file and defined in a non-header file. I do *not*
get this link error if I define the template function directly in the
header file, or if the change the function to non-template.
For example...
*** Why am I getting a link error for template function f() in the code
below?
Thanks!
Suzanne
-----------------------------------
// File: Main.cpp
#include "Templates.h"
int main(int argc, char** argv) {
f<int>(5); // <--link error here
}
-----------------------------------
// File: Templates.h
#ifndef TEMPLATES_H
#define TEMPLATES_H
#ifndef _STD_USING
#define _STD_USING // Must be #define'd in order to include iostream.h.
#endif // _STD_USING
#include <iostream>
template<class T>
void f(T n);
#endif // TEMPLATES_H
-----------------------------------
// File: Templates.cpp
#include "Templates.h"
template<class T>
void f(T n) {
std::cout << "I got an \"n\"!\n";
}