|
-
July 18th, 2002, 07:16 AM
#1
templating a class member function!
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;
}
-
July 18th, 2002, 07:39 AM
#2
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 18th, 2002, 07:50 AM
#3
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.
-
July 18th, 2002, 08:32 AM
#4
Fair enough, but point 1 is still valid.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 18th, 2002, 08:51 AM
#5
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...
-
July 18th, 2002, 08:58 AM
#6
It looks like a compiler bug. Are you using VC++?
ZDF
What is good is twice as good if it's simple.
"Make it simple" is a complex task.
-
July 18th, 2002, 09:05 AM
#7
Yes, it is a bug! Works fine with Borland 5.5 and g++ 2.95.3-5.
Regards,
ZDF
What is good is twice as good if it's simple.
"Make it simple" is a complex task.
-
July 18th, 2002, 09:09 AM
#8
-
July 18th, 2002, 09:34 AM
#9
Ah, well. VC++ is seriously brain-damaged when it comes to template member functions.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 22nd, 2002, 10:34 AM
#10
Actually the compiler deduces the type of GetSize() by itself. If you call GetSize() without the explicit template-argument you'll be fine!
-
July 24th, 2002, 11:47 AM
#11
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)"
-
July 24th, 2002, 11:50 AM
#12
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
-
July 25th, 2002, 08:42 AM
#13
Thanks to all, this solved my problem.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|