* a compiation issue with gcc
@ 2005-05-19 12:13 Damanjit Singh
2005-05-19 12:36 ` Peter Doerfler
2005-05-19 13:12 ` Eljay Love-Jensen
0 siblings, 2 replies; 3+ messages in thread
From: Damanjit Singh @ 2005-05-19 12:13 UTC (permalink / raw)
To: gcc-help
The following code does not compile with gcc (ggc -c simple.cpp) and
gives the following errors ( where as the code compiles in windows
VC++ )-
Could someone tell me the reason for this, and if there is an
available workaround.
Thanks,
#include "iostream.h"
#include "stdio.h"
template <class T,void (*DestroyCallback)( T )> class B
{
private:
int a;
};
template <class T> class A
{
private:
static void ASFreeWrapper( T Ptr )
{
free( Ptr );
}
public:
typedef B<T,&ASFreeWrapper> Type;
};
int main()
{
A<int *> a;
return 0;
}
/* Here is the error reported by gcc -
simple.cpp:9: warning: all member functions in class `A<T>' are private
simple.cpp: In instantiation of `A<int*>':
simple.cpp:21: instantiated from here
simple.cpp:16: invalid use of undefined type `class A<int*>'
simple.cpp:9: declaration of `class A<int*>'
*/
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: a compiation issue with gcc
2005-05-19 12:13 a compiation issue with gcc Damanjit Singh
@ 2005-05-19 12:36 ` Peter Doerfler
2005-05-19 13:12 ` Eljay Love-Jensen
1 sibling, 0 replies; 3+ messages in thread
From: Peter Doerfler @ 2005-05-19 12:36 UTC (permalink / raw)
To: gcc-help
Hi Damanjit.
As far as I understand there is some confusion with pointers to member
functions esp. if they are static depending on the compiler.
I managed to get your code compiled by removing the & in
> typedef B<T,&ASFreeWrapper> Type;
Can't recall which is correct but I think the version without &.
and of course including malloc.h for free().
This is with gcc4.0.1 on a x86 system.
On my gentoo version of gcc3.4.3 I get an ICE, but I guess that is a known
issue. Does anybody know?
If you are interested: I found
http://www.codeproject.com/cpp/FastDelegate.asp
an informative read. Goes into detail what the standard says about member
function pointers and what different compilers actually implement.
Hope this helps
- Peter
On Thursday 19 May 2005 14:12, Damanjit Singh wrote:
> #include "iostream.h"
> #include "stdio.h"
> template <class T,void (*DestroyCallback)( T )> class B
> {
> private:
> int a;
> };
> template <class T> class A
> {
> private:
> static void ASFreeWrapper( T Ptr )
> {
> free( Ptr );
> }
> public:
> typedef B<T,&ASFreeWrapper> Type;
>
> };
> int main()
> {
> A<int *> a;
> return 0;
> }
On Thursday 19 May 2005 14:12, Damanjit Singh wrote:
> The following code does not compile with gcc (ggc -c simple.cpp) and
> gives the following errors ( where as the code compiles in windows
> VC++ )-
> Could someone tell me the reason for this, and if there is an
> available workaround.
> Thanks,
>
> #include "iostream.h"
> #include "stdio.h"
> template <class T,void (*DestroyCallback)( T )> class B
> {
> private:
> int a;
> };
> template <class T> class A
> {
> private:
> static void ASFreeWrapper( T Ptr )
> {
> free( Ptr );
> }
> public:
> typedef B<T,&ASFreeWrapper> Type;
>
> };
> int main()
> {
> A<int *> a;
> return 0;
> }
>
> /* Here is the error reported by gcc -
> simple.cpp:9: warning: all member functions in class `A<T>' are private
> simple.cpp: In instantiation of `A<int*>':
> simple.cpp:21: instantiated from here
> simple.cpp:16: invalid use of undefined type `class A<int*>'
> simple.cpp:9: declaration of `class A<int*>'
> */
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: a compiation issue with gcc
2005-05-19 12:13 a compiation issue with gcc Damanjit Singh
2005-05-19 12:36 ` Peter Doerfler
@ 2005-05-19 13:12 ` Eljay Love-Jensen
1 sibling, 0 replies; 3+ messages in thread
From: Eljay Love-Jensen @ 2005-05-19 13:12 UTC (permalink / raw)
To: Damanjit Singh, gcc-help
Hi Damanjit,
I see a few unrelated problems with your code.
1)
#include "iostream.h" is not C++ compliant (it's deprecated at best). Use #include <iostream>. Since it wasn't used in your code example, I removed it.
2)
#include "stdio.h" is not C++ compliant (it's deprecated at best). Use #include <cstdio>. Since it wasn't used in your code example, I removed it.
3)
With GCC, you should compile C++ code with the g++ front end, not with gcc.
Now on to the meat of the problem.
4)
You appear to be replicating std::auto_ptr, which is part of Standard C++ in the C++ <memory> header. But maybe you have a simplified example for illustrative purposes, so just consider this an FYI.
5)
You were missing the header for free(). I added #include <cstdlib>. Instead of using <cstdlib>'s malloc / free for your heap memory management, I strongly recommend using C++ new / delete. But that may be an aside from your actual problem at hand.
6)
You are passing in a function pointer as a template type parameter. I vaguely recall there can be issues with function pointers (especially ones that are static-at-the-translation-unit level, or static-at-the-class-level).
As an alternative for you consideration (which will be more digestible / less problematic amongst a variety of C++ compilers at different levels of C++ compliance), you could use a functor object. Functor objects tend to be a lot more C++ friendly in general, anyway -- but may or may not be suitable for your needs.
That could look something like this:
--------------------------------------
#include <cstdlib> // free
template <class T, class DestroyFunctor>
class B
{
private:
T a;
};
template <class T>
class A
{
private:
void operator ()(T Ptr)
{
free(Ptr);
}
public:
typedef B<T, A<T> > Type;
};
int main()
{
A<int*> a;
return 0;
}
--------------------------------------
HTH,
--Eljay
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-05-19 13:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-19 12:13 a compiation issue with gcc Damanjit Singh
2005-05-19 12:36 ` Peter Doerfler
2005-05-19 13:12 ` Eljay Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).