gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:11:35 24 4
gpt4 key购买 nike

我在编译代码时从 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类型在文件外的原因吗?

最佳答案

你得到一个错误

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

关于c++ - 函数(给出 mpz_t)未在此范围内声明 c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32798565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com