Aren't you sure that the second red one (int) shouldn't be T?
Anyway, I've been experimenting around with what works and what doesn't and I have put together this program which demonstrates that. Note though that this code excerpt deals simply with template functions and not with the special case of template function for which I asked for help. As it turned out, the other template functions didnt work either. How can I fix it up this code so that the template functions Swap_B and Swap_C work properly?
Code:
// C Board Ex.cpp : Defines the entry point for the console application.
// Programming Environment: Microsoft Visual Studio 6.0 (Visual C++ in this case)
// Includes
#include "stdafx.h" // Contains statements: #pragma once, #include <iostream> and #include <tchar.h>
#include "Swap.h"
// Functions
template <typename T> void Swap_A(T *Ptr_A,T *Ptr_B);
// Main function
int main(void)
{
// Declare variables
int a,b;
double A=1.1,B=2.2;
// Test functions
Other_Function(&a,&b); // Fine: Was declared in header at file scope and so extern was unnecessary. Assigns values 1 to a and 2 to b.
printf("a=%d b=%d A=%3f B=%f\n",a,b,A,B);
Swap_A<int>(&a,&b); // Fine: Local function which proves that template functions work in my programming environment. Swaps values of a and b
Swap_A<double>(&A,&B); // Fine: Ditto. Swaps values of A and B
printf("a=%d b=%d A=%3f B=%f\n",a,b,A,B);
Swap_B<int>(&a,&b); // Fine: <int> version of Swap_B was specifically called in Swap.cpp in Other_Function() and so no error occurs. Why?? If that function call is commented then this comes up as an error too.
Swap_B<double>(&A,&B); // Error LNK2019: unresolved external symbol "void __cdecl Swap_B<double>(double *,double *)" (??$Swap_B@N@@YAXPAN0@Z) referenced in function _main
printf("a=%d b=%d A=%3f B=%f\n",a,b,A,B);
Swap_C<int>(&a,&b); // Error LNK2019: unresolved external symbol "void __cdecl Swap_C<int>(int *,int *)" (??$Swap_C@H@@YAXPAH0@Z) referenced in function _main
Swap_C<double>(&A,&B); // Error LNK2019: unresolved external symbol "void __cdecl Swap_C<double>(double *,double *)" (??$Swap_C@N@@YAXPAN0@Z) referenced in function _main
printf("a=%d b=%d A=%3f B=%f\n",a,b,A,B);
// Delay exit
printf("Press enter to exit!");
fgetc(stdin);
// Return a value
return 0;
}
// Swap function
template <typename T> void Swap_A(T *Ptr_A,T *Ptr_B)
{
// Perform swap
T Temp=*Ptr_A;
*Ptr_A=*Ptr_B;
*Ptr_B=Temp;
}
// EOF
// Swap.cpp : Contains Swap_B function
//
// Includes
#include "stdafx.h"
#include "Swap.h"
// Functions
template <typename T> void Swap_B(T *Ptr_A,T *Ptr_B)
{
// Perform swap
T Temp=*Ptr_A;
*Ptr_A=*Ptr_B;
*Ptr_B=Temp;
}
template <typename T> void Swap_C(T *Ptr_A,T *Ptr_B)
{
// Perform swap
T Temp=*Ptr_A;
*Ptr_A=*Ptr_B;
*Ptr_B=Temp;
}
void Other_Function(int *a,int *b)
{
*a=2;
*b=1;
Swap_B<int>(a,b); // Call Swap_B function but not Swap_C function to see the difference that makes
}
// EOF
// Swap.h : Declares Swap_B function
//
// Ensure that code is only executed once
#ifndef SWAP_H
#define SWAP_H
// Functions
template <typename T> extern void Swap_B(T *Ptr_A,T *Ptr_B);
template <typename T> extern void Swap_C(T *Ptr_A,T *Ptr_B);
void Other_Function(int *a,int *b);
// Endif
#endif /*SWAP_H*/
// EOF
I think that what i am doing is aptly described by the comments. I coloured the important parts to make them easier to find. If there is anything else that you need to know, please just ask. I can't explain what's going on...
Thx,
Philipp