|
-
May 28th, 2012, 05:14 PM
#1
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.
-
May 28th, 2012, 05:38 PM
#2
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.
-
May 28th, 2012, 06:00 PM
#3
Re: Setting function return type
Sorry, I was trying something else before I posted this and forgot to change it
-
May 28th, 2012, 07:36 PM
#4
Re: Setting function return type
-
May 28th, 2012, 11:57 PM
#5
Re: Setting function return type
 Originally Posted by SamstaUK
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
-
May 29th, 2012, 04:37 AM
#6
Re: Setting function return type
 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.
 Originally Posted by Paul McKenzie
Also, what is mpz_t?
An opaque pointer type.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|