CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2009
    Posts
    38

    Setting function return type

    I want my function to return the type mpz_t, but I'm not sure how?

    I've tried:
    mpz_t MyFunction(mpz_t A, mpz_t B){}

    But it didn't work, here is my code so far, I have bolded the parts of the code which are causing errors and added the errors in the comments:

    Code:
    #include <iostream>
    #include <mpir.h>
    
    using namespace std;
    
    ???? A(mpz_t m, mpz_t n){
    	mpz_t mmo; //used to store the value of m, minus one
    	mpz_t nmo; //used to store the value of n, minus one
    	mpz_t one; //used to store the value 1
    	mpz_init(mmo);
    	mpz_init(nmo);
    	mpz_init(one);
    	mpz_set_ui(one, 1);
    
    	if(mpz_cmp_ui(m, 0) == 0){
    		mpz_add_ui(n, n, 1);
    		return n; //error: return value type does not match the function type
    	}
    
    	if(mpz_cmp_ui(m, 0) > 0 && mpz_cmp_ui(n, 0) == 0){
    		mpz_sub_ui(mmo, m, 1);
    		return A(mmo, one);
    	}
    
    	if(mpz_cmp_ui(m, 0) > 0 && mpz_cmp_ui(n, 0) > 0){
    		mpz_sub_ui(mmo, m, 1);
    		mpz_sub_ui(nmo, n, 1);
    		return A(mmo, A(m, nmo)); 
    	}
    }
    
    int main(){
    	mpz_t param1;
    	mpz_init(param1);
    	mpz_t param2;
    	mpz_init(param2);
    
    	mpz_set_ui(param1, 4);
    	mpz_set_ui(param2, 2);
    
    	mpz_out_str(stdout, 10, A(param1, param2)); 
    	return 0;
    }
    Is there a way around this?

    Thank you
    Last edited by SamstaUK; May 28th, 2012 at 06:00 PM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Setting function return type

    There's a disconnect between what you say you tried and what you implemented. Your implementation shows a void function trying to return something.

  3. #3
    Join Date
    Apr 2009
    Posts
    38

    Re: Setting function return type

    Sorry, I was trying something else before I posted this and forgot to change it

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Setting function return type

    What is A?

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

    Re: Setting function return type

    Quote Originally Posted by SamstaUK View Post
    I want my function to return the type mpz_t, but I'm not sure how?
    Why are you returning a variable name?
    Code:
    mpz_t MyFunction(mpz_t A, mpz_t B){}
    //...
    return A(mmo, one);   // ?
    The A is the name of the variable, not the type. Aren't you supposed to do something like this?
    Code:
    return mpz_t(mmo, one);
    ?
    Also, what is mpz_t? If it's a class or struct, you should be passing them by reference or const reference, not by value, unless there is a compelling and logical reason to be passing them by value.
    Code:
    mpz_t MyFunction(mpz_t& A, mpz_t& B)
    Regards,

    Paul McKenzie

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

    Re: Setting function return type

    Quote Originally Posted by SamstaUK
    I want my function to return the type mpz_t, but I'm not sure how?
    Read the GMP manual on parameter conventions:
    When a function is going to return a GMP result, it should designate a parameter that it sets, like the library functions do. More than one value can be returned by having more than one output parameter, again like the library functions. A return of an mpz_t etc doesn't return the object, only a pointer, and this is almost certainly not what's wanted.
    Also, note that what you mpz_init, you must mpz_clear, similiar to the rule that what you malloc, you must free. Alternatively, you can use the C++ wrapper and let RAII do the work for you.

    Quote Originally Posted by Paul McKenzie
    Also, what is mpz_t?
    An opaque pointer 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

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