CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 1999
    Location
    Malaysia
    Posts
    224

    error in C++ but not in C

    Hello
    I have a C header file from someone else, and i need to complile with the Lib. I am using VC++ 6 and give me alot of error. Same header file, if use in C program, only warning not error.

    The error is
    error C2440: 'initializing' : cannot convert from 'const int' to 'MASK_TYPE'


    Here i attach the header file, which is come with a lib file.



    #if !defined( MASKS_DEFINED )

    #define MASKS_DEFINED

    typedef enum {GAUSSIAN, LOW1, LOW2, NEIGHBORHOOD, LAP1, LAP2, LAPB1, LAPB2,
    SOB_V, SOB_H, W1, W2, W3, W4, W5, W6, W7, W8, W9} MASK_TYPE;

    typedef struct {
    short int n;
    short int **vertical;
    short int **horizontal;
    short int **diag1;
    short int **diag2;
    } KIRSCH_MASK;

    #define MASKS1 0, 1.0,2.0,1.0, 2.0,4.0,2.0, 1.0,2.0,1.0, \
    1, 0.1,0.1,0.1, 0.1,0.2,0.1, 0.1,0.1,0.1, \
    2, 1.0/16,1.0/8.0,1.0/16.0, 1.0/8.0,1.0/4.0,1.0/8.0, 1.0/16,1.0/8,1.0/16, \
    3, 1.0/9.0,1.0/9.0,1.0/9.0, 1.0/9.0,1.0/9.0,1.0/9.0, 1.0/9.0,1.0/9.0,1.0/9.0, \
    4, 0.0,-1.0,0.0, -1.0,4.0,-1.0, 0.0,-1.0,0.0, \
    5, 1.0,-2.0,1.0, -2.0,4.0,-2.0, 1.0,-2.0,1.0, \
    6, -1.0,-1.0,-1.0, -1.0,9.0,-1.0, -1.0,-1.0,-1.0, \
    7, 1.0,-2.0,1.0, -2.0,5.0,-2.0, 1.0,-2.0,1.0, \
    8, 1.0,0.0,-1.0, 2.0,0.0,-2.0, 1.0,0.0,-1.0, \
    9, 1.0,2.0,1.0, 0.0,0.0,0.0, -1.0,-2.0,-1.0, \
    10, 1.0,1.4142136,1.0, 0.0,0.0,0.0, -1.0,-1.4142136,-1.0, \
    11, 1.0,0.0,-1.0, 1.4142136,0.0,-1.4142136, 1.0,0.0,-1.0, \
    12, 0.0,-1.0,1.4142136, 1.0,0.0,-1.0, -1.4142136,1.0,0.0, \
    13, 1.4142136,-1.0,0.0, -1.0,0.0,1.0, 0.0,1.0,-1.4142136, \
    14, 0.0,1.0,0.0, -1.0,0.0,-1.0, 0.0,1.0,0.0, \
    15, -1.0,0.0,1.0, 0.0,0.0,0.0, 1.0,0.0,-1.0, \
    16, 1.0,-2.0,1.0, -2.0,4.0,-2.0, 1.0,-2.0,1.0, \
    17, -2.0,1.0,-2.0, 1.0,4.0,1.0, -2.0,1.0,-2.0, \
    18, 1.0,1.0,1.0, 1.0,1.0,1.0, 1.0,1.0,1.0

    #define MASKS2 0, 1.0/25,1.0/25,1.0/25,1.0/25,1.0/25, \
    1.0/25,1.0/25,1.0/25,1.0/25,1.0/25, \
    1.0/25,1.0/25,1.0/25,1.0/25,1.0/25, \
    1.0/25,1.0/25,1.0/25,1.0/25,1.0/25, \
    1.0/25,1.0/25,1.0/25,1.0/25,1.0/25

    static struct mask3x3 {
    MASK_TYPE choice;
    float kernel[9];
    } MASK3x3[] = {MASKS1}; //1st ERROR is HERE

    static struct mask5x5 {
    MASK_TYPE choice;
    float kernel[25];
    } MASK5x5[] = {MASKS2};

    extern Image * h_image(int type, unsigned int height, unsigned int width);

    #endif /* MASKS_DEFINED */







    Sooner Or Later, Everyone Does...

  2. #2

    Re: error in C++ but not in C

    why you not replace 0 by GAUSSIAN etc. into #define?
    t!


  3. #3
    Join Date
    Jan 2000
    Location
    Canada
    Posts
    42

    Re: error in C++ but not in C

    It might be possible the syntax of defining few types might be different.try using.

    #ifdef __cplusplus
    extern "C" {
    #endif

    your .h file

    #ifdef __cplusplus
    }
    #endif

    bye now




  4. #4
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043

    Re: error in C++ but not in C

    Replace the first 0 in MASKS1 and MASKS2 definition to GAUSSIAN and it should work...

    #define MASKS1 GAUSSIAN, 1.0, 2.0, ....
    ....
    #define MASKS2 GAUSSIAN, 1.0/25,1.0/25, ....
    ....



    Martin


  5. #5
    Join Date
    Apr 1999
    Location
    Malaysia
    Posts
    224

    Re: error in C++ but not in C

    Thanks for you suggestion, but it is not working.
    The error still there. Any other idea?
    TQ

    Sooner Or Later, Everyone Does...

  6. #6
    Join Date
    Apr 1999
    Location
    Malaysia
    Posts
    224

    Re: error in C++ but not in C

    Thanks for your reply, but i don't really understand how to do this. Can you please explain this in detail? TQ

    Sooner Or Later, Everyone Does...

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: error in C++ but not in C

    It is obvious what the error is.

    #define MASKS1 0, // etc...
    1, // etc...
    2, // etc...



    The 0, 1, 2, 3, etc. in your #defines MASKS1 can't be used. Why? Because C++ is looking for a MASK_TYPE, not an integer. Look at your structure:

    static struct mask3x3 {
    MASK_TYPE choice; // <<-- This *must* be a MASK_TYPE, not an integer!
    float kernel[9];
    } MASK3x3[] = {MASKS1};



    You are attempting to initialize the array of structures with an integer followed by 9 floats. But the structure plainly states a MASK_TYPE followed by 9 floats. Therefore replace all of your 0, 1, 2, 3, etc. in your #define MASKS1 with GAUSSIAN, LOW1, LOW2, etc. You did define them, so why don't you use them?

    Regards,

    Paul McKenzie


  8. #8
    Join Date
    Dec 1999
    Location
    Israel
    Posts
    2,851

    Re: error in C++ but not in C

    As a result of improved C++ conformance, some constructs that used to compile will now give errors. One compilation error that has changed is c2440. In C you were able to implicitly cast an integer to an enum, this is no longer possible. The solution to your problem is one out of two:
    1. Use the name of the enum instead of the constant:

    #define MASKS1 GAUSSIAN,... LOW1,...LOW2



    2. explictly cast the integer to the enum

    #define MASKS1 (MASK_TYPE)0,... (MASK_TYPE)1,...(MASK_TYPE)2



    note that the former is the prefered method because it is type safe, and you won't get any errors caused by casting an integer to a value the enum doesn't contain(W9 + 1).

    Got a question? try looking it up in MSDN first. Msdn comes with the Visual Studio, and can be found at http://msdn.microsoft.com
    ---===---
    I'm not here for the rates, but rating a post is a good way for me to know how much i helped.
    ---===---
    Daniel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured