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

    [RESOLVED] Problem with macros for creating get/set functions

    "error C2084: function 'D3DXVECTOR3 FName(int)' already has a body"
    I am using Visual Studio 2005 professional.

    I don't understand why FName (function name) is taken directly as the function name instead of being replaced with the macro argument.

    Code:
    // Actual syntax
    #define Get_InsProp3(FName,PName,PPart1,PPart2,PPart3,DType,DefVal) inline DType FName (int Instance) { Object_Instance* pInstance; if (GameEngine_Object_Instance_IsValid(Instance)) { pInstance = (Object_Instance*)Instance; return DType(pInstance->##PName##.##PPart1, pInstance->PName##.##PPart2, pInstance->PName##.##PPart3); } else { ReportBadReference_Instance(L#FName, Instance); return DType##DefVal; } };ReportBadReference_Instance(L#FName, Instance); return DefVal; } };
    
    // Simplified with multiple lines
    #define Get_InsProp3(FName,PName,PPart1,PPart2,PPart3,DType,DefVal)
    inline DType FName (int Instance) {
        Object_Instance* pInstance;
        if (GameEngine_Object_Instance_IsValid(Instance)) {
            pInstance = (Object_Instance*)Instance;
            return DType(pInstance->##PName##.##PPart1, pInstance->PName##.##PPart2, pInstance->PName##.##PPart3);
        } else {
            ReportBadReference_Instance(L#FName, Instance); return DType##DefVal;
        }
    };
    Last edited by Dawoodoz; August 26th, 2010 at 01:27 PM. Reason: Code tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Problem with macros for creating get/set functions

    Please, look at your post and, please, tell us: can you read/understand this macro definition?
    I can't.
    Please, edit your post adding Code tags and indentations.
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2010
    Posts
    88

    Re: Problem with macros for creating get/set functions

    Can you read it now.
    Last edited by Dawoodoz; August 26th, 2010 at 01:28 PM. Reason: I have figured out how to use it.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Problem with macros for creating get/set functions

    No. The second line is toooooooooo long.
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2010
    Posts
    88

    Re: Problem with macros for creating get/set functions

    The second line is the same code as below. Just ignore it if you can't read it.

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Problem with macros for creating get/set functions

    I tested the second variant (with \ added) in VS2005 and it worked like a charm.
    Have you tried setting your compiler to pre-process to file?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  7. #7
    Join Date
    Jul 2010
    Posts
    88

    Re: Problem with macros for creating get/set functions

    Pre processing to file gave a lot of empty lines but could be useful for debugging if I know what to search for.

    This seems to do the same thing but with the same error.
    Code:
    #define Get_InsProp3(FName,PName,PPart1,PPart2,PPart3,DType,DefVal)\
    inline DType FName (int Instance) {\
    	Object_Instance* pInstance;\
    	if (GameEngine_Object_Instance_IsValid(Instance)) {\
    		pInstance = (Object_Instance*)Instance;\
    		return DType(pInstance->PName##.##PPart1, pInstance->PName##.##PPart2, pInstance->PName##.##PPart3);\
    	} else {\
    		ReportBadReference_Instance(L#FName, Instance);\
    		return DType##DefVal;\
    	}\
    };
    I need something to use with FName to make the precompiler see that it is an argument and not a string.

  8. #8
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Problem with macros for creating get/set functions

    What, exactly, are you trying to do? Create a macro that creates a function "on the fly"? If so, why not try re-writing the function as a template?

    Viggy

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

    Re: Problem with macros for creating get/set functions

    Quote Originally Posted by Dawoodoz View Post
    Pre processing to file gave a lot of empty lines but could be useful for debugging if I know what to search for.
    The idea is, you turn on the 'send preprocessor output to file' option (or whatever it's called), then you can see (instead of wonder or guess) exactly what your '#define Get_InsProp3' macro is expanding to.

    If you already did this & still have questions, then post what it expanded to here.

  10. #10
    Join Date
    Jul 2010
    Posts
    88

    Re: Problem with macros for creating get/set functions

    Quote Originally Posted by MrViggy View Post
    What, exactly, are you trying to do? Create a macro that creates a function "on the fly"? If so, why not try re-writing the function as a template?

    Viggy
    I have a lot of get and set functions for my graphics engine and need macros for generating the functions.

    This is how one of the final get set interfaces should be in the end.
    Code:
    D3DXVECTOR3 GameEngine_Object_Instance_GetPosition(int Instance) {
    	Object_Instance* pInstance;
    	if (GameEngine_Object_Instance_IsValid(Instance)) { pInstance = (Object_Instance*)Instance;
    		return D3DXVECTOR3(pInstance->transformation._41, pInstance->transformation._42, pInstance->transformation._43);
    	} else {
    		ReportBadReference_Instance(L"GameEngine_Object_Instance_GetPosition", Instance);
    		return D3DXVECTOR3(0.0f, 0.0f, 0.0f);
    	}
    }
    void GameEngine_Object_Instance_SetPosition(int Instance, D3DXVECTOR3 NewPosition) {
    	Object_Instance* pInstance;
    	if (GameEngine_Object_Instance_IsValid(Instance)) { pInstance = (Object_Instance*)Instance;
    		pInstance->transformation._41 = NewPosition.x;
    		pInstance->transformation._42 = NewPosition.y;
    		pInstance->transformation._43 = NewPosition.z;
    	} else {
    		ReportBadReference_Instance(L"GameEngine_Object_Instance_SetPosition", Instance);
    	}
    }

  11. #11
    Join Date
    Jul 2010
    Posts
    88

    Re: Problem with macros for creating get/set functions

    Of what I understand about function templates, they can only be used for taking multiple datatypes for one function name.

  12. #12
    Join Date
    Jul 2010
    Posts
    88

    Re: Problem with macros for creating get/set functions

    I have found the error. It was a side effect of another macro so that other macros crashed.
    Last edited by Dawoodoz; August 27th, 2010 at 08:40 AM. Reason: Found the bug

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