LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2007, 03:19 AM   #1
Division_By_Zero
LQ Newbie
 
Registered: May 2007
Posts: 3

Rep: Reputation: 0
Problem compiling code with g++ that works for gcc


Hi!

I have written a program that used a c-lib (libnids) compiles in gcc and runs perfectly. But i am not able to compile the same code in g++. There is a single error, the error is in the line

error: invalid conversion from ‘void*’ to ‘void (*)()’

in my code: (in main)
nids_params.syslog = (void*)nids_new_syslog;

defined in nids.h as a member in a struct: (pointer to function)
void (*syslog)();

the function definition:
void nids_new_syslog(int type, int errnum, struct ip *iph, void *data)
{ // do something }

I do a cast like (void*)nids_new_syslog, but it don't works.

Any idea what i can do???

Thanks in advance.

Last edited by Division_By_Zero; 05-18-2007 at 03:24 AM.
 
Old 05-18-2007, 08:30 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
What you are trying to do is cast from “void (*)(int, int, ip*, void*)” to “void (*)()”. In C, you used a cast to “void *” as a crutch (i.e., instead of casting to “void (*)()”, you casted to the most general case (“void *”), and decided to let the compiler handle the cast to “void (*)()”). In C++, implicit casts from type “void *” are not allowed (one of the subtle differences — this is why you must make explicit casts when using malloc in C++).

What I think you want to do is:
Code:
nids_params.syslog = (void (*)())nids_new_syslog;
 
Old 05-21-2007, 02:19 AM   #3
Division_By_Zero
LQ Newbie
 
Registered: May 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Smile

Thank you osor!

It works.

Now I understood it.
 
  


Reply

Tags
c++, cast



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
XAWTV works, videodog works, motion works but how to code my own? rylan76 Linux - Hardware 0 01-06-2006 06:30 AM
A GCC code generation problem? dogbird Programming 4 12-09-2005 11:52 AM
Problem in executing code in gcc gcc_learner Programming 3 03-26-2005 03:28 AM
Problem compiling code in g++ works for gcc juan_de_margo Programming 3 02-24-2005 03:41 AM
gcc warning compiling C code vose Programming 2 09-29-2004 10:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration