Thread: C to C++ migration for typedef struct

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Cool C to C++ migration for typedef struct

    I have the following code that define the camera control feature in *.h . The code was written in C. However for my project, I use C++ (VS C++ 2005). The problem is when I force the VS to use C compiler, this code works fine. But when I use C++ compiler, I have the error from PCAM_FEATURE_CONTROL_STRUCT.

    Code:
    // Camera control feature structure
    typedef struct 
    {
        int   SliderID;
        int   AutoCheckID;
        int   OnePushButtonID;
        int   MinValueID;
        int   MaxValueID;
        int   CurrentValueID;
        int   InqOID;
        int   ControlOID;    void    (*ControlOnAuto)(PCAM_FEATURE_CONTROL_STRUCT , HWND , PFIREi_CAMERA_FEATURE_CONTROL_REGISTER);
        void    (*ControlOnOnePush)(PCAM_FEATURE_CONTROL_STRUCT , HWND , PFIREi_CAMERA_FEATURE_CONTROL_REGISTER);
    } CAM_FEATURE_CONTROL_STRUCT, *PCAM_FEATURE_CONTROL_STRUCT;
    the compile error message is
    Code:
    c:\controler.h(82) : error C2061: syntax error : identifier 
    'PCAM_FEATURE_CONTROL_STRUCT'
    It seems that the problem is about forward reference. Since PCAM_FEATURE_CONTROL_STRUCT is used before it is defined later.

    How can I modify this structure to work with C++ compiler? Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You don't need to typecast structs in C++.
    Code:
    struct CAM_FEATURE_CONTROL_STRUCT
    {
        int   SliderID;
        int   AutoCheckID;
        int   OnePushButtonID;
        int   MinValueID;
        int   MaxValueID;
        int   CurrentValueID;
        int   InqOID;
        int   ControlOID;
        void    (*ControlOnAuto)(PCAM_FEATURE_CONTROL_STRUCT , HWND , PFIREi_CAMERA_FEATURE_CONTROL_REGISTER);
        void    (*ControlOnOnePush)(PCAM_FEATURE_CONTROL_STRUCT , HWND , PFIREi_CAMERA_FEATURE_CONTROL_REGISTER);
    };
    Is fine.
    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.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by alexy01 View Post
    It seems that the problem is about forward reference. Since PCAM_FEATURE_CONTROL_STRUCT is used before it is defined later.

    How can I modify this structure to work with C++ compiler? Thanks

    Where is it 'defined later' and can that definition be moved ahead prior to this struct's declaration (or a forward declaration)?

    [edit]Nevermind, didn't see the second part of the initial typedef.[/edit]
    Last edited by hk_mp5kpdw; 02-12-2008 at 12:53 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I don't think the code is even valid ISO C, even if the compiler accepts it. Anyway, Elysia's way ought to work.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CornedBee View Post
    I don't think the code is even valid ISO C, even if the compiler accepts it.
    I think I missed that part. But this was discussed before, wasn't it? If a struct refers to itself, it must first have an actual name, so you access it through its name and not the typedef.
    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.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    If a struct refers to itself, it must first have an actual name, so you access it through its name and not the typedef.
    Thanks Elysia.
    Could you point me to the thread that we have the discussion on this topic? ( I am very new here)
    Or can you show me the brief example how to write it? Thanks man.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by alexy01 View Post
    How can I modify this structure to work with C++ compiler? Thanks
    Like this:

    Code:
    // Camera control feature structure
    typedef struct CAM_FEATURE_CONTROL_STRUCT
    {
        int   SliderID;
        int   AutoCheckID;
        int   OnePushButtonID;
        int   MinValueID;
        int   MaxValueID;
        int   CurrentValueID;
        int   InqOID;
        int   ControlOID;    void    (*ControlOnAuto)(CAM_FEATURE_CONTROL_STRUCT *, HWND , PFIREi_CAMERA_FEATURE_CONTROL_REGISTER);
        void    (*ControlOnOnePush)(CAM_FEATURE_CONTROL_STRUCT *, HWND , PFIREi_CAMERA_FEATURE_CONTROL_REGISTER);
    } *PCAM_FEATURE_CONTROL_STRUCT;
    Changed areas in red. If your goal is simply to get it to compile, that is probably enough.

    However, the callback members of this struct should be hinting to you to create a CAM_FEATURE_CONTROL_STRUCT base class with virtual functions ControlOnAuto() and ControlOnOnePush(), and to derive from this to produce concrete instances implementing this interface. C-style callbacks are not a good thing in C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM