Is it possible to write a function that returns a variable type? My guess would have been to use template functions, although the rest of my program is in C (I have a mixed C/C++ programming environment so I can still use C++ in parts). The following (unrelated) code snippet works for example:
but when I try something like this it doesn't work:Code:template <typename T> void Swap(T *Ptr_A,T *Ptr_B) { // Declare variables T Temp; // Perform swap Temp=*Ptr_A; *Ptr_A=*Ptr_B; *Ptr_B=Temp; }
The compiler tells me:Code:template <typename T> T Comb_C(int M,int R) { // Declare variables T Answer=1; // Work out mCr for(int i=1;i<=R;i++) { Answer*=(M+1)-i; Answer/=i; } // Return answer return Answer; }
when I doerror LNK2019: unresolved external symbol "int __cdecl Comb_C<int>(int,int)" (??$Comb_C@H@@YAHHH@Z) referenced in function _mainI have declared the function, in exactly the same way I had declared the swap function, so I must assume that that isn't the problem.Code:Comb_C<int>(a,b)
I do realise that something like this would work IF I only need the different types to be different types of int (int8,int16,int32,int64) OR diff. types of floating point (float,double):
Is there a way to do it with template functions though, where I could theoretically have the output as an integer or double or whatever?Code:#define COMB_C(T,M,R) ((T) Comb_C((M),(R))) ... int main() { ... printf("%d\n",COMB_C(__int32,12,4)); ... } ... __int64 Comb_C(int M,int R) { __int64 Answer=1; for(int i=1;i<=R;i++) { Answer*=(M+1)-i; Answer/=i; } return Answer; }
Thx,
Philipp



LinkBack URL
About LinkBacks



