gpt4 book ai didi

C++ - 初始化静态私有(private)类成员时出现类型不匹配错误

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

我有一个类,我希望它能够静态生成自身的随机实例。为此,我有一个 std::uniform_real_distribution 的静态实例。但是,我在编译器中收到类型不匹配错误。

编译器错误是:“class Foo”中的“genId”未命名类型“class Foo”中的“randId”未命名类型。但是,我已经在头文件中指定了类型,如下所示。

头文件(Foo.hpp):

class Foo {
public:
static std::random_device rdId;
static std::mt19937 genId;
static std::uniform_real_distribution<> randId;
// other code
}

实现文件(Foo.cpp):

#include "Foo.hpp"

Foo::genId = std::mt19937(Foo::rdId());
Foo::randId = std::uniform_real_distribution<>(0, 100);
// other code

为什么我已经声明了类型会出现这个错误?

最佳答案

需要指定类型:

std::mt19937 Foo::genId = std::mt19937(Foo::rdId());
std::uniform_real_distribution<> Foo::randId = std::uniform_real_distribution<>(0, 100);

关于C++ - 初始化静态私有(private)类成员时出现类型不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42475301/

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