Konrad Zuse
Konrad Zuse

Reputation: 3

function (gives mpz_t) not declared in this scope c++

I get following error from g++ while compiling my code:

    main.cpp:4:35: error: ‘createBInt’ declared as function returning an array
 mpz_t createBInt(unsigned long int);
                                   ^
main.cpp:6:41: error: ‘createBInt’ declared as function returning an array
 mpz_t createBInt(unsigned long int value) { //creates a mpz_t from unsigned long int
                                         ^
main.cpp: In function ‘int main()’:
main.cpp:14:28: error: ‘createBInt’ was not declared in this scope
     mpz_t i1 = createBInt(5); //init mpz_t with 5 
                            ^

my code:

    #include <iostream>
#include "gmp.h"

mpz_t createBInt(unsigned long int);

mpz_t createBInt(unsigned long int value) { //creates a mpz_t from unsigned long int
    mpz_t i1;
    mpz_init (i1);
    mpz_set_si(i1,value);
    return i1;
}
int main()
{
    mpz_t i1 = createBInt(5); //init mpz_t with 5 
    std::cout << i1 << "\n";  //output
}

The Code is very simple. It only creates a mpz_t (from the gmp.h). I don't understand why there is an error. Is it why the type mpz_t is outside the file?

Upvotes: 0

Views: 286

Answers (1)

melpomene
melpomene

Reputation: 85767

You get an error

error: ‘createBInt’ declared as function returning an array

because you can't return arrays from functions in C++.

mpz_t is declared as:

typedef __mpz_struct mpz_t[1];

or something like that; that is, it's a typedef for a 1-element array.

You can do this:

void createBInt(mpz_t i1, unsigned long int value) { //creates a mpz_t from unsigned long int
    mpz_init (i1);
    mpz_set_si(i1,value);
}

Upvotes: 3

Related Questions