illegal use of this type as an expression
-
Hi all, I'm new to C++. I've a stupid problem. This is the code I used:
class A { int x; }; class B { template<class T> void temp(void){ }; void test(void); }; void B::test(void) { temp<A>(); };
Why can't I compile this code? The error message is: test.cpp(23) :error C2275: 'A' : illegal use of this type as an expression test.cpp(23) : error C2059: syntax error : ')' If I move the template function temp() out from class B, then it is ok. Could someone help me? Thanks a lot. -
Hi all, I'm new to C++. I've a stupid problem. This is the code I used:
class A { int x; }; class B { template<class T> void temp(void){ }; void test(void); }; void B::test(void) { temp<A>(); };
Why can't I compile this code? The error message is: test.cpp(23) :error C2275: 'A' : illegal use of this type as an expression test.cpp(23) : error C2059: syntax error : ')' If I move the template function temp() out from class B, then it is ok. Could someone help me? Thanks a lot.Which compiler do you use?
-
Hi all, I'm new to C++. I've a stupid problem. This is the code I used:
class A { int x; }; class B { template<class T> void temp(void){ }; void test(void); }; void B::test(void) { temp<A>(); };
Why can't I compile this code? The error message is: test.cpp(23) :error C2275: 'A' : illegal use of this type as an expression test.cpp(23) : error C2059: syntax error : ')' If I move the template function temp() out from class B, then it is ok. Could someone help me? Thanks a lot.it works fine in VS 2005, but fails in VC6. not sure why it fails.
image processing toolkits | batch image processing | blogging
-
Hi all, I'm new to C++. I've a stupid problem. This is the code I used:
class A { int x; }; class B { template<class T> void temp(void){ }; void test(void); }; void B::test(void) { temp<A>(); };
Why can't I compile this code? The error message is: test.cpp(23) :error C2275: 'A' : illegal use of this type as an expression test.cpp(23) : error C2059: syntax error : ')' If I move the template function temp() out from class B, then it is ok. Could someone help me? Thanks a lot.Are you wanting something like:
class A
{
int x;
};class B
{
template<class T> void temp(const T& data){ }
void test(void);
};void B::test(void)
{
A a;
temp(&a);
};
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
it works fine in VS 2005, but fails in VC6. not sure why it fails.
image processing toolkits | batch image processing | blogging
VC6 has problems when a template type parameter doesn't appear in the function's parameter list, as is the case in hsuch's sample code. The usual workaround is to add a dummy
T*
param that defauls toNULL
.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?
-
Hi all, I'm new to C++. I've a stupid problem. This is the code I used:
class A { int x; }; class B { template<class T> void temp(void){ }; void test(void); }; void B::test(void) { temp<A>(); };
Why can't I compile this code? The error message is: test.cpp(23) :error C2275: 'A' : illegal use of this type as an expression test.cpp(23) : error C2059: syntax error : ')' If I move the template function temp() out from class B, then it is ok. Could someone help me? Thanks a lot.If you're using VC6, change it to
template<class T> void temp(T* = NULL) { }
to work around a compiler bug.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?
-
VC6 has problems when a template type parameter doesn't appear in the function's parameter list, as is the case in hsuch's sample code. The usual workaround is to add a dummy
T*
param that defauls toNULL
.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?
you mean like this?
class A
{
int x;
};class B
{
template < class T > void temp(T *)
{
}
void test(void);
};void B::test(void)
{
A z;
temp< A >(&z);
};that doesn't work either. edit: adding the "T* = NULL" works, though
image processing toolkits | batch image processing | blogging
-
you mean like this?
class A
{
int x;
};class B
{
template < class T > void temp(T *)
{
}
void test(void);
};void B::test(void)
{
A z;
temp< A >(&z);
};that doesn't work either. edit: adding the "T* = NULL" works, though
image processing toolkits | batch image processing | blogging
See my response below.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?
-
Which compiler do you use?
-
If you're using VC6, change it to
template<class T> void temp(T* = NULL) { }
to work around a compiler bug.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?