lachy
lachy

Reputation: 2073

C++ - Type mismatch error when initialising static private class member

I have a class that I would like to be able to statically generate random instances of itself. To do this I have a static instance of std::uniform_real_distribution. However, I am getting a type mismatch error in the compiler.

The compiler errors are: 'genId' in 'class Foo' does not name a type and 'randId' in 'class Foo' does not name a type. However, I have specified the type in the header file as can be seen below.

The header file (Foo.hpp):

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

The implementation file (Foo.cpp):

#include "Foo.hpp"

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

Why does this error occur when I have already declared the type?

Upvotes: 0

Views: 101

Answers (1)

Emil Laine
Emil Laine

Reputation: 42838

You need to specify the type:

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

Upvotes: 2

Related Questions