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 ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    6

    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.

    The existing code is:

    Code:
    typedef struct xycoord
    {
    	int x, y;
    } coord;
    My new code is:

    Code:
    class coord
    {
    public:
    	int x, y;
    
    	coord(int xin, int yin) { x = xin; y = yin; }
    	coord();
    	void operator=(coord &rhs);
    	coord operator+(coord &other);
    	bool operator==(coord &other);
    };
    
    void					coord::operator=(coord &rhs)
    {
    	x = rhs.x;
    	y = rhs.y;
    }
    
    coord					coord::operator+(coord &other)
    {
    	return coord(x + other.x, y + other.y);
    }
    
    bool					coord::operator==(coord &other)
    {
    	return (x == other.x && y == other.y);
    };
    I know the new code I've written will compile when I put it in a fresh C++ project. However, when I try to compile it in the old program, I get thousands of errors.

    Compiling...
    makedefs.c
    c:\...\coord.h(20) : error C2061: syntax error : identifier 'coord'
    c:\...\coord.h(20) : error C2059: syntax error : ';'
    c:\...\coord.h(21) : error C2449: found '{' at file scope (missing function header?)
    c:\...\coord.h(31) : error C2059: syntax error : '}'
    c:\...\coord.h(39) : error C2061: syntax error : identifier 'coord'
    c:\...\coord.h(39) : error C2059: syntax error : ';'
    ...

    I'm sure I'm missing something fundamental. Please help. I don't know where to look.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Well, you can't compile C++ code with a C compiler...

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    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.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Can you post the exact, full contents of the file? It will help identify the lines the errors occur on as well as possible causes.

    It's also possible that a different file included by the source file being compiled has a problem that doesn't show up until here.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    6

    coord.h

    See attached file.
    Attached Files Attached Files

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    Quote Originally Posted by HagopH View Post
    "Project -> Properties -> Configuration Properties -> C/C++ -> Advanced -> Compile As -> Compile as C++ Code (/TP)" is set.
    Even better, rename the file as .cpp.
    And why the long whitespace?
    Code:
    void					coord::operator=(coord &rhs)
    It just makes it harder to read. A space is sufficient.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    The file that contains the code I'm working on is named "coord.h". My code does not contain a corresponding coord.c or coord.cpp file. Is there a naming convention for C++ headers?

    The long whitespace is for readability when I collapse my functions. Visual C++ collapses functions to their header and makes it easy to look at all your functions in a file. For example:

    Code:
    void			functiona(register struct blah *rtmp)
    static int		functionb()
    struct blah *		functionc(register struct obj *otmp, coord xy, bool foo)
    If I just put a space between the return type and the function header, I see:

    Code:
    void functiona(register struct blah *rtmp)
    static int functionb()
    struct blah * functionc(register struct obj *otmp, coord xy, bool foo)
    I've seen others use new lines:

    Code:
    void
    functiona(register struct blah *rtmp)
    static int
    functionb()
    struct blah *
    functionc(register struct obj *otmp, coord xy, bool foo)
    Which choice is most readable?

    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.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    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.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    Quote Originally Posted by HagopH View Post
    Which choice is most readable?
    To me? The second.


    I'm guessing you're getting compile errors because some source files are compiling as C and parsing that C++ header.
    Try adding

    #ifndef __cplusplus
    #error "Header must be compiled as C++"
    #endif

    To the top of the header and see if you get an error. If you do, you've found the culprit, since classes aren't supported in C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Elysia,

    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.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    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.

    It depends on how your code looks like.

    #ifdef __cplusplus
    #pragma comment("My C++ code goes here")
    #else
    #pragma comment("My C code goes here")
    #endif
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by HagopH View Post
    Elysia,

    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:
    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    ... bunch of functions here....
    #ifdef __cplusplus
    }
    #endif
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  2. abstract class
    By xddxogm3 in forum C++ Programming
    Replies: 5
    Last Post: 01-01-2005, 09:08 AM
  3. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 08:57 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21