Template function parameters

From: nevlis (silven_at_removesilven.canada.com)
Date: 09/27/04


Date: Mon, 27 Sep 2004 14:45:12 GMT

I am trying to create a Template function that will take
differnet parameter types. However all my attempts have failed
can someone please tell me what I am doing wrong here is the code.

It seems to work when I pass integers however will not compile when
I have a float or double

I get this error when compiling

initializing : cannot convert from 'unsigned short *' to 'double'

I do not understand why it has unsigned short when I am passing
a double for the intData parameters

//function prototype
template <class Z>void StoreInArray(VARIANT* arr_rgElems,const
inDataType, Z inData, int intPosition);

//Test Calling this works
strcpy (strAsterisk, "*");
strTemp = A2BSTR(strAsterisk);
StoreInArray(rgElems,VT_BSTR, strTemp, intCounter++);

//this works
StoreInArray(rgElems,VT_I2, 99, intCounter++);

//I tried different many differnet ways but always get
//the same type of error when compiling

double tempX = 8.99
StoreInArray<double>(rgElems,VT_R8, tempX, intCounter++);
StoreInArray<double>(rgElems,VT_R8, (double)tempX, intCounter++);
StoreInArray(rgElems,VT_R8, (double)tempX, intCounter++);

//function
template <class Z> void StoreInArray(VARIANT* arr_rgElems,const
inDataType, Z inData, int intPosition){

        COleDateTime myDateTime;

        double douInData = inData;
        
        VariantInit(&arr_rgElems[intPosition]);
        arr_rgElems[intPosition].vt = inDataType;
            switch (inDataType) {
                case VT_BSTR:
                        arr_rgElems[intPosition].bstrVal = (BSTR) inData;
                        break;

                case VT_I2:
                        arr_rgElems[intPosition].intVal = (int)inData;
                        break;

                case VT_I4:
                        arr_rgElems[intPosition].intVal = (long)inData;
                        break;

                case VT_R4:
                        arr_rgElems[intPosition].fltVal = (float)inData;
                        break;

                case VT_R8:
                        arr_rgElems[intPosition].dblVal = (double)inData;
                        break;

                case VT_DATE:
                        myDateTime = (time_t) *(long*)inData;
                        arr_rgElems[intPosition].date = (DATE)myDateTime;
                        break;

                default:
                        break;
        }
}

Thanks

nevlis



Relevant Pages

  • Re: Is export "useless and broken"?
    ... >> template once, and would thus hopefully be faster. ... to only compile one of the source files which triggered the ... instantiation, ... compiling one source file is faster than compiling a dozen. ...
    (comp.lang.cpp)
  • Template problem when migrating from VS.NET to VS2003
    ... In VS.NET I made a + operator for STL list iterators by writing the function ... When I tried compiling this under VS2003 I got the warning ... template <class Type,class Allocator,class Distance> typename ...
    (microsoft.public.vc.language)
  • templates and translation units
    ... Some time ago I had two different problems in compiling my code using ... class DataBase; ... // friend template functions must be forward declared ... The two messages from the linker are exactly the ones the linker was ...
    (comp.lang.cpp)
  • Re: Template class to allocate 2D/3D dynamic memory
    ... The reason for the source getting compiled when you moved everything to a single file is that the compiler found the usage-pattern for the template. ... The template is a shell, the compiler, when compiling the ADynamicallocator.cpp file doesn't know what fits into the shell and so can't generate the code for the specialized case. ... By moving the entire contents to a single file, you provided with the specialization and the template at the same instant, and hence the linker was able to link your code. ... template class CADynamicAllocator; at the end of your ADynamicallocator.cpp file should fix the code. ...
    (microsoft.public.vc.mfc)
  • Re: Template function parameters
    ... >>differnet parameter types. ... > to know what compiler version you're using, and it would be even more ... > You should not be converting inData to some other type, ... > this function a template. ...
    (microsoft.public.vc.language)