Click to See Complete Forum and Search --> : templating a class member function!


Ardiani
July 18th, 2002, 07:16 AM
Im trying to write a template member function but something goes wrong. I get this compiler error:

error C2275: "MY_STRUCT" : illegal use of this type as an expression

Can anybody help me?

////////////////////////////////////////////////////////////////////////
// MyClass.h

typedef struct
{
int n;
long l;
} MY_STRUCT;

class MyClass
{
public:
template <typename T> int GetSizeOf(T myStruct);
MyClass();
virtual ~MyClass();
};
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
// MyClass.cpp

MyClass::MyClass()
{
}

MyClass::~MyClass()
{
}

template <typename T> int MyClass::GetSizeOf(T myStruct)
{
return sizeof(T);
}
////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
// Main.cpp

void main ()
{
MyClass TestObj;
MY_STRUCT myStruct;

int nSize = TestObj.GetSizeOf<MY_STRUCT>(myStruct);

return;
}

Graham
July 18th, 2002, 07:39 AM
1) You can't put the definition of GetSizeOf in the .cpp file - it has to go in the header.

2) Why have you made it a class member? The implementation you've given (apart from resolving to sizeof) would be better off as a free function.

Ardiani
July 18th, 2002, 07:50 AM
Actually the sample I've posted used to be just an example of a problem. The reason I made this function a class member is that I have to use some member variables in it.

Graham
July 18th, 2002, 08:32 AM
Fair enough, but point 1 is still valid.

Ardiani
July 18th, 2002, 08:51 AM
Well, I've put the definition of 'GetSizeOf' function in the header file, unfortunately I'm still getting the same compiler error if I try using it in main function. Thank you anyway...

zdf
July 18th, 2002, 08:58 AM
It looks like a compiler bug. Are you using VC++?

zdf
July 18th, 2002, 09:05 AM
Yes, it is a bug! Works fine with Borland 5.5 and g++ 2.95.3-5.

Regards,

Ardiani
July 18th, 2002, 09:09 AM
Yes. I'm using VC++6.0.

Graham
July 18th, 2002, 09:34 AM
Ah, well. VC++ is seriously brain-damaged when it comes to template member functions.

amag
July 22nd, 2002, 10:34 AM
Actually the compiler deduces the type of GetSize() by itself. If you call GetSize() without the explicit template-argument you'll be fine!

Ardiani
July 24th, 2002, 11:47 AM
Maybe I didn't get it right. Do you mean something like this:

MyClass TestObj;
MY_STRUCT myStruct;
myStruct.l = 1;
myStruct.n = 2;

int nSize = TestObj.GetSizeOf(myStruct);


I get linker error:
unresolved external symbol "public: int __thiscall MyClass::GetSizeOf(struct MY_STRUCT)"

jfaust
July 24th, 2002, 11:50 AM
As Graham said, "You can't put the definition of GetSizeOf in the .cpp file - it has to go in the header."

This is where your unresolved is coming from.

Jeff

Ardiani
July 25th, 2002, 08:42 AM
Thanks to all, this solved my problem.:p :p :p