CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    May 2010
    Posts
    9

    problem returning a typedef type?

    Alright so I'm working on a project right now in which I have written the following code for easy transition between data types:

    //so that I can change the data type when I want to
    typedef float value;

    //so that I can change this function when I want to as well
    #define val_sqrt sqrtf;


    And I plan to use these throughout my project. Unfortunately, I've run into a bit of a problem.
    Say I have the following function definition:

    value SomeClass::ReturnSqrt(void) const
    {
    return val_sqrt(a_*a_ + b_*b_);
    }


    Because the sqrtf function returns a float and not my defined 'value', I keep getting this error:

    'return' : cannot convert from 'float (__cdecl *)(float)' to 'value'


    What I'd like to ask is am I missing something important? Or how do I fix what I'm doing incorrectly?? Thanks in advance for the help!

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: problem returning a typedef type?

    Looks like it thinks that you are trying to return the pointer to the function, not the resolved function. Where did you define sqrtf?

    Why did you make your own square root function anyway? The standard library has its own square root function called sqrt.

  3. #3
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: problem returning a typedef type?

    Try changing this:
    #define val_sqrtf sqrtf;

    To this:
    #define val_sqrtf(x) sqrtf(x);

  4. #4
    Join Date
    May 2010
    Posts
    9

    Re: problem returning a typedef type?

    Sorry for the confusion, I forgot to specify that I am using the standard library function for sqrtf. I didn't create a new sqrtf function.

    So when it calls my 'val_sqrt', it's really just looking at the sqrtf function? In that case, is there any way to redefine the return type for the sqrtf function?

    What I though was that because I had type-defined 'float' as another type 'value', was that every 'float' in the program would be seen as my 'value', but I suppose this isn't the case with functions I haven't created. Do you know of any way to get around this?

  5. #5
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: problem returning a typedef type?

    ...and remove the semicolon at the end of the define. It doesn't seem to hurt for the snippet I tested but it should be removed anyway.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: problem returning a typedef type?

    Incidentally, have you considered the use of templates?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    May 2010
    Posts
    9

    Re: problem returning a typedef type?

    Ok, thanks for the help Martin O. That fixed my problem, no problem.

    And no one wants two semi-colons on the same line, thanks for the tip!

    I'm still somewhat new to programming (about a year or so now) but for my personal growth, what is that code doing on a lower level? Is there a simple way to explain why and how this works?

    laserlight: As for templates, because of limited experience with them it slipped my mind, but I think I can see your point. Those could be very useful for what I'm trying to do. Thanks.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: problem returning a typedef type?

    Quote Originally Posted by Akira__tyler
    And no one wants two semi-colons on the same line, thanks for the tip!
    for loop

    Quote Originally Posted by Akira__tyler
    what is that code doing on a lower level? Is there a simple way to explain why and how this works?
    You are basically defining a function-style macro named val_sqrtf such that occurrences of val_sqrtf(x) will be replaced by sqrt(x) during the macro expansion phase, where x is the "argument".

    By the way, it is a common convention to fully capitalise macro names, especially since macro names do not obey the rules of scope.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: problem returning a typedef type?

    Quote Originally Posted by Akira__tyler View Post
    I'm still somewhat new to programming (about a year or so now) but for my personal growth, what is that code doing on a lower level? Is there a simple way to explain why and how this works?
    Assuming you're referring to why #define val_foo(x) foo(x) works but #define val_foo foo doesn't, then..
    I don't know exactly, it has something to do with how the preprocessor works. You can probably find detailed info on it in your compilers manual. I try to avoid using #defines wherever possible myself. For instance, in your case, instead of the #define I'd just make a wrapper method:

    Code:
    float val_sqrtf(float x) { return sqrtf(x); }
    and just use my IDE's 'find and replace in files' function to replace all sqrtf's with val_sqrtf.

  10. #10
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: problem returning a typedef type?

    Quote Originally Posted by laserlight View Post
    You are basically defining a function-style macro named val_sqrtf such that occurrences of val_sqrtf(x) will be replaced by sqrt(x) during the macro expansion phase, where x is the "argument".
    Oh. Thanks.

  11. #11
    Join Date
    Jan 2009
    Posts
    1,689

    Re: problem returning a typedef type?

    Quote Originally Posted by Akira__tyler View Post
    Sorry for the confusion, I forgot to specify that I am using the standard library function for sqrtf. I didn't create a new sqrtf function.
    Oh, I didn't even know that function existed. I used to use in in C, but when I switched to C++, I just used sqrt. In C++ sqrt is overloaded to accept any type.

  12. #12
    Join Date
    May 2010
    Posts
    9

    Re: problem returning a typedef type?

    Alright, thanks again for all of the help!

    Let's just see if I really understand this though. So, because I am using a function style macro to pass the type and because the standard library sqrt function is templated, the sqrt function is taking my passed data type 'value' and compiling a sqrt function to fit my data type?

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: problem returning a typedef type?

    Firstly, you need to understand that macro expansion happens before the routine work of compilation. You can imagine the preprocessor going through your source code and textually making those replacements. So if you write:
    Code:
    #define abc(x) xyz(x)
    Then you can imagine that code such as:
    Code:
    abc("hello world");
    is textually replaced by:
    Code:
    xyz("hello world");
    in your source code, before the compiler begins its real work.

    Now, the C++ standard library has overloaded std::sqrt for certain types. If your value type is not among them, then it will be ambiguous as to which overload you are trying to call. If you use sqrtf, then it is not ambiguous (if I remember correctly), but it may not be the desired function corresponding to your value type.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

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