C++ Error C2440 when using exisiting C source

I am having an issue where I have a C2440 error when compiling a C++ project in VS2005. The error is due some existing C code in another project which I depend on which casts a void pointer to either a char or int pointer.

The code resembles:

void * bbb;
... // some code which defines the void pointer
int * aaa = bbb;

However in C++ I need to specifically cast the type to be valid such as:

int * aaa = (int *)bbb;

My question is whether there exists a flag or compile option in VS2005 which allows my to compile my main project in C++ and ignore this error from depending projects I want to compile as C?

I would rather not change any of the original source as it is a shared project.

2 Answers

  1. Bill- Reply


    If you name the file "something.c", the compiler will [unless explicitly told otherwise] compile it as C (and thus happily accept your pointer conversion without using a cast). A file named "something.cpp" will be compiled as C++, and you will need a cast to convert a pointer to a different type, even if it's a void pointer.

  2. Bob- Reply


    The only way to do this was to edit the header files which caused the problem. It turned out to be a minor number of files which could be merged back into the shared projects.

    There is no way to get around this in VisualC++. I had used -fpermissive to ignore the error in gcc.

    So I just made the update to:

    int * aaa = (int *)bbb;

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>