gpt4 book ai didi

c++ - 当调用不在函数内部时出现类型未找到错误

转载 作者:行者123 更新时间:2023-11-27 22:58:11 26 4
gpt4 key购买 nike

我试图弄清楚为什么会出现此错误,但没有成功。

当我尝试编译这段代码时

using namespace std;
#include <iostream>
#include <gmp.h>
#include <gmpxx.h>

class MyRand
{
gmp_randclass randGen(gmp_randinit_default);
};


int main()
{
MyRand s();
gmp_randclass gmpRand(gmp_randinit_default);

return 0;
}

使用此命令 g++ Random.cpp -lgmpxx -lgmp,我收到以下消息:

In file included from Random.cpp:3:0: Random.cpp:8:27: error: ‘__gmp_randinit_default’ is not a type gmp_randclass randGen(gmp_randinit_default);

但是,请注意,这一行

gmp_randclass randGen(gmp_randinit_default);

和这个一样(在main函数里面)

gmp_randclass gmpRand(gmp_randinit_default);

并且只有第一个会产生错误。

此外,如果我按如下方式定义类 MyRand(在函数内初始化 mpz_randclass)

class MyRand
{
void func()
{
gmp_randclass randGen(gmp_randinit_default);
}
};

我可以毫无错误地编译它。

有人知道这是怎么回事吗?

非常感谢。

最佳答案

您不能在类成员定义的地方对其进行初始化(至少在 C++11 之前不能)。你可以把它放在构造函数中。

class MyRand
{
public:
MyRand() : randGen(gmp_randinit_default) {
}
private:
gmp_randclass randGen;
};

关于c++ - 当调用不在函数内部时出现类型未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30496322/

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