函数(给出mpz_t)未在此范围内声明c ++

时间:2015-09-26 15:04:57

标签: c++

编译代码时,我从g ++中得到以下错误:

    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 
                            ^

我的代码:

    #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
}

守则很简单。它只创建一个mpz_t(来自gmp.h)。 我不明白为什么会有错误。 这是mpz_t类型在文件外面的原因吗?

1 个答案:

答案 0 :(得分:3)

您收到错误

error: ‘createBInt’ declared as function returning an array

因为您无法从C ++中的函数返回数组。

mpz_t声明为:

typedef __mpz_struct mpz_t[1];

或类似的东西;也就是说,对于1元素数组,它是typedef

你可以这样做:

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);
}