Migrating from C, want to implement class

This is a discussion on Migrating from C, want to implement class within the C++ Programming forums, part of the General Programming Boards category; I'm trying to update a C program written 20+ years ago. I want to use current compilers and standards. I'm ...

Migrating from C, want to implement class

I'm trying to update a C program written 20+ years ago. I want to use current compilers and standards. I'm looking at this as a good learning process, beyond reading C++ programming guides and reading code that has no real-world applications. I've already updated all the function headers to current ANSI standard headers. I'm now trying to write a class with functions.

I'm using Visual C++ 2008 to compile. Is there a setting in the compiler I have to change in order to compile C++ code? "Project -> Properties -> Configuration Properties -> C/C++ -> Advanced -> Compile As -> Compile as C++ Code (/TP)" is set.

The entire problem is that makedefs is a .c file rather than .cpp file, so the compiler tries to compile it as C - whilst HakonH's suggestion is technically feasible, it's a much easier if you rename all .c files to .cpp - the reason I say ALL is that the rules for names when calling external functions are different in C and C++, so unless you have really strong motives for it, you should compile the whole project as either C or C++, not a mix of the two.

If you really MUST compile one portion of source as C and another as C++, then you should use extern "C" in the prototypes of C functions when included into C++.

As for matsp's suggestion that I rename all my code files .CPP, I just tried it and received many compiler errors, some involving flex code files, others involving reserved C++ keywords (class), some having to do with the project's implementation of "boolean", and yet others about being unable to convert from void* to char*.

I'm trying to transition this code from C to C++. I was hoping that I could take little steps, one at a time, rather than having to make drastic changes to the entire code all at once. I've been making sure that I can compile and run this program after each significant change I make so I know that nothing I've done breaks everything. While my goal would be to eventually rename all 100+ .c files to .cpp and make sure that everything that's being used conforms to C++ standards and OOD, I am not arrogant enough to think that a beginning programmer could possibly understand everything that's going on in the 150,000+ lines of code in this project. I do not want to discount the knowledge, experience, and efforts of dozens of other programmers on multiple platforms who have written a compilable and working program, regardless of how many bugs may exist in it. In my book, compilable and working trumps elegant and irreparable. Some things I do may introduce other bugs, but my goal is to make it easy for other beginners to be able to read and understand what's going on.

No, header files are compiled in accordance to the file that includes the header file, e.g. x.c will compile the header as C, whilst y.cpp including the same header will compile it as C++. Which is why I suggested that you need to change your .c files to .cpp - in visual studio, you are probably best off starting a new empty project, moving all your code to the new directory of that project (and renaming at the same time), then importing the new source and header files.

There are other ways you can do it, but that's the safest, even if it's not the easiest.

Why do you suggest I'd be, "best off starting a new empty project?" I have a project that's working and compiling in Visual C++ now. What benefit would there be in moving to the new empty project? I'm realizing the problem I'm experiencing is due to a mix of C and C++, but there are other parts of this project that are using OOD. How is it that C and C++ can coexist in some places and when I'm trying to introduce a class in C++, I'm having difficulty. What topic should I learn or understand before I can make this happen? I've searched help files, online tutorials, and C programming guides. My problem is that I don't know what I don't know.

Yes, C and C++ _can_ be made to co-exist within the same project - but it's harder to work that way, since you need to make sure that all your code knows whether it's calling C or C++ - there are also some pitfalls in this area if you start using C++ exceptions.

You don't have to do it that way, it's just harder work to delete all the .c files (from the project, but not from the filesystem), then rename the files themselves, and then add them back in.

Of course, if you have a large project with many different files, then you are probably better off with a gradual approach - in which case I would suggest that you make a "toy-project" with some 3-4 files, some which are C++ and some which are C, and try to make that project communicate between the different source files - that will be a more manageable experiment than doing so with say 20 or 200 files.

You've hit the nail on the head. I got that error. Now, how do I insert C++ code into my predominantly C project? I don't want to redo everything at once. I'd prefer to introduce C++ code and use it as required.

There can be two approaches. If your C code needs some C code inside that header, you could use #ifdefs to separate C and C++ code.
Otherwise you should remove the #include to that header in your C code.

You've hit the nail on the head. I got that error. Now, how do I insert C++ code into my predominantly C project? I don't want to redo everything at once. I'd prefer to introduce C++ code and use it as required.

The code that is C++ needs to go into a file called *.cpp (where * is whatever makes sense for what that file does).

If you then also need to call C code from the C++ code, you will have to declare the C functions something like this:

Code:

extern "C" int myCFunction(int arg);

You can declare a bunch of files with

Code:

extern "C" {
... bunch of functions here....
}

And if you want it to also be compatible with compilers that don't understand C++, you should do somethng along these lines: