LinuxQuestions.org
Help answer threads with 0 replies.
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 01-22-2011, 10:42 PM   #1
girlsmakegraves
LQ Newbie
 
Registered: Dec 2005
Posts: 5

Rep: Reputation: 0
Post Error when converting C to C++


Hello,

I'm working on a project where I am converting a C file to C++. I have worked out 99% of the errors but there is still one lingering one that I cant seem to figure out.

I get the following error when compiling with g++
Code:
mscp.cpp:749: error: invalid conversion from ‘const void*’ to ‘move*’
mscp.cpp:750: error: invalid conversion from ‘const void*’ to ‘move*’
The offending lines are in this function:

Code:
static int cmp_move(const void *ap, const void *bp)
{
        struct move *a = ap;
        struct move *b = bp;

        if (a->prescore < b->prescore) return -1;
        if (a->prescore > b->prescore) return 1;
        return a->move - b->move; /* this makes qsort deterministic */    
}
I would rather if someone not directly gave me the answer but rather pushed me in the right direction.

What I can tell is that it is trying to convert from one datatype to another which was allowed in c but is invalid in cpp.
 
Old 01-22-2011, 11:07 PM   #2
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
you may want to read this
http://www.cplusplus.com/doc/tutorial/typecasting/
 
Old 01-22-2011, 11:58 PM   #3
girlsmakegraves
LQ Newbie
 
Registered: Dec 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you for that article. It cleared up a lot of my confusion. So from what I understood from that is that C++ requires some sort of type casting before assigning void* (void pointers) to other data types.

So I did this:
Code:
static int cmp_move(const void *ap, const void *bp)
{
        struct move *a = (short*)ap;
        struct move *b = (short*)bp;

        if (a->prescore < b->prescore) return -1;
        if (a->prescore > b->prescore) return 1;
        return a->move - b->move; /* this makes qsort deterministic */    
}
but now I'm receiving the following errors:

Code:
mscp.cpp:749: error: cannot convert ‘short int*’ to ‘move*’ in initialization
mscp.cpp:750: error: cannot convert ‘short int*’ to ‘move*’ in initialization
I don't exactly understand how I went wrong here. Any help would be appreciated.
 
Old 01-23-2011, 01:12 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by girlsmakegraves View Post
Thank you for that article. It cleared up a lot of my confusion. So from what I understood from that is that C++ requires some sort of type casting before assigning void* (void pointers) to other data types.

So I did this:
Code:
static int cmp_move(const void *ap, const void *bp)
{
        struct move *a = (short*)ap;
        struct move *b = (short*)bp;

        if (a->prescore < b->prescore) return -1;
        if (a->prescore > b->prescore) return 1;
        return a->move - b->move; /* this makes qsort deterministic */    
}
but now I'm receiving the following errors:

Code:
mscp.cpp:749: error: cannot convert ‘short int*’ to ‘move*’ in initialization
mscp.cpp:750: error: cannot convert ‘short int*’ to ‘move*’ in initialization
I don't exactly understand how I went wrong here. Any help would be appreciated.

If you look at your

Code:
struct move *a = (short*)ap;
, you'll see that 'a' is pointer to 'move' struct while because of '(short*)' '(short*)ap' is pointer to 'short'. So, by your own construction, figuratively speaking you try to assign pointer to oranges to pointer to apples, and C++ justly objects. It objects because dereferencing pointer apples being actually pointer to oranges will yield unpredicted and quite possibly crashing/crushing results.
 
Old 01-23-2011, 02:55 AM   #5
girlsmakegraves
LQ Newbie
 
Registered: Dec 2005
Posts: 5

Original Poster
Rep: Reputation: 0
I realized I had to make it (move*) not (short*)

and that worked.

Thanks for all the assistance. I appreciate the reading provided too.
 
Old 01-23-2011, 03:55 AM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Also note you really should mark a and b const, too. It allows the compiler to optimize code more aggressively, but it also helps you catch programming errors -- say, if in a later version you tried to modify something pointed by a or b. Because you cast it to a non-const type, the compiler will allow it, but the users of the function would assume otherwise because the content at ap and bp is marked const. (With aggressive optimization, the compiler may trust the declaration, and assume the values are not changed. This may result in very strange behaviour, very difficult bugs to pinpoint.)

Since you already realized the problem, let me push you a bit further. This is how you really should write the function:
Code:
static int cmp_move(const void *const ap, const void *const bp)
{
    const struct move *const a = (const struct move *const)ap;
    const struct move *const b = (const struct move *const)bp;

    if (a->prescore < b->prescore) return -1;
    if (a->prescore > b->prescore) return 1;
    return a->move - b->move;
}
The above version adds the const qualifier to ap and bp, to a and b, and to data pointed to by a and b.

Here's the three lines that help me get the const qualifiers right:
Code:
const char *s = ...; /* Data pointed to by s is constant. s++ is allowed, s[0]++ is not allowed. */
char *const s = ...; /* Pointer s is constant. s++ is not allowed, s[0]++ is allowed. */
const char *const s = ...; /* Pointer s and the data it points to are both constant. Neither s++ nor s[0]++ is allowed. */
Hope this helps you write better code,
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 06:02 AM.
 
  


Reply



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
Converting m4b to mp3, error with libmp3lame aphesia Linux - Software 1 01-14-2011 08:57 AM
converting c code to c++ causes an type error knobby67 Programming 3 03-25-2010 08:02 AM
Error When converting Routing OpenVPN to bridge mode openvpn danmartinj Linux - Software 0 11-06-2009 09:23 AM
converting *.avi to *.mpeg error ceantuco Linux - Newbie 12 05-20-2009 04:13 AM
error converting jpeg to ai file aeshj Linux - General 3 10-16-2007 10:28 AM

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

All times are GMT -5. The time now is 04:40 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