Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

template function in a class 1

Status
Not open for further replies.

liwen2w

Programmer
Jun 24, 2004
2
US
Hi guys,

I had a problem by using microsoft visual C++ 6.0 to compiling a simple code. using template function in a class seems not work.

Everything works fine by using g++ compiler.

Does anyone have some solution for this problem?

Thanks in advance.

Liwen


The error message as following

Compiling...
testit.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\tmp\testit.cpp(26) : error C2275: 'u8_t' : illegal use of this type as an expression
C:\Program Files\Microsoft Visual Studio\MyProjects\tmp\testit.cpp(4) : see declaration of 'u8_t'
Error executing cl.exe.


The source code:


#include <iostream.h>

#if defined(_M_IX86)
typedef unsigned __int8 u8_t;
#endif

class test{
template <class T>
int getSize(T const &val)
{
return sizeof(val);
}
};

template <class T>
int getSize(T const &val)
{
return sizeof(val);
}

int main()
{
test tt;
int t=tt.getSize< u8_t >(0); //error here
int tmp=getSize< u8_t >(0);
cout << tmp << " : " << sizeof( __int8 ) << endl;
return 0;
}
 
Visual C++ 6.0's compiler doesn't understand explicit template function calls.
All template function calls must be implicit, i.e. in your case :

Code:
 int t=tt.getSize (0);

--
Globos
 
Globos,

Thank you very much. I got what I want to know.

Liwen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top