473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Typecast illegal from int to pointer?

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 ;)
Jun 4 '06 #1
2 4188
Ulrich Hobelmann wrote:
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?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 4 '06 #2
Phlip wrote:
Ulrich Hobelmann wrote:
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?


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.
Jun 4 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Lars Plessmann | last post by:
I have a problem with typecast methods. Here is a setter of an object: function setKind($kind) { if (is_int((int)$kind) and (strlen($kind)<=6)) { $this->kind = $kind; return true;
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
8
by: Trishia Rose | last post by:
this is something ive always wondered, does it take cpu time at run time to typecast or just at compile time? for example consider the two little bits of code: int a = 5; int b = a; and: ...
3
by: Ray Mitchell | last post by:
Hello, I have an array list whose elements are the following types: float int float int ....etc.
9
by: Roshni | last post by:
Hi, I wanted to know how do function pointers sometime access illegal memory access ? Could any one give me an example ? Thanks, Roshni
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
11
by: Manikandan | last post by:
Hi, I am using the following snippet to compare an object with integer in my script. if ( $forecast < 4 ) { I got the "segmentation fault" error message when i executed this script in CLI....
3
by: ryan.gilfether | last post by:
I have a problem that I have been fighting for a while and haven't found a good solution for. Forgive me, but my C++ is really rusty. I have a custom config file class: class ConfigFileValue...
18
by: Richard Eich | last post by:
I need a clue. I'm getting an "invalid lvalue in increment" from gcc 4.1.2 on this line... sum += *((unsigned short *)iphp)++ ; ....but I don't see what the problem is. gcc 3.4.3 isn't...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.