Typecast illegal from int to pointer?

  • Thread starter Ulrich Hobelmann
  • Start date
U

Ulrich Hobelmann

Hi, I admit that this isn't great C++, but I'm reusing a container class
I wrote in C, which stores void * elements. Sometimes I insert pointers
into the container, sometimes ints (both are 32bit values, no problem).

Now I ported the thing to C++, and wrapped it in a template to get rid
of the while cast-to-void*/cast-from-void* mess. Works.

The problem is that I just tried to insert an int (i.e. instantiated the
template with something that's not a pointer), and my compiler (GCC 4) says:
In member function 'void List<T>::add(T) [with T = int]':
error: invalid conversion from 'int' to 'void*'
error: initializing argument 1 of 'void list::add(void*)'

(where: List<typename T> is wrapping my class "list")

Why can't i cast an int to void *? It's not pretty, but at least I'm
doing it explicitly, and no C dialect should get in my way ;)
 
P

Phlip

Ulrich said:
Why can't i cast an int to void *? It's not pretty, but at least I'm
doing it explicitly, and no C dialect should get in my way ;)

I can't see the line involved. Did you forget to paste it in, or am I
tripping again?
 
U

Ulrich Hobelmann

Phlip said:
I can't see the line involved. Did you forget to paste it in, or am I
tripping again?

Oh sorry, I thought I had an explicit cast in there, but in fact I used
C++'s implicit cast (to void *). Works fine now.

Only the extractor (iterator) already had a cast to T in there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top