0

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?

2
  • 1
    You need to specify the type in the implementation file, too Commented Feb 26, 2017 at 23:10
  • @DanielJour am wondering why this is the case?
    – lachy
    Commented Feb 26, 2017 at 23:19

1 Answer 1

2

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);
1
  • Thanks @tuple_cat, is there any specific reason why I need to specify the type when I have already declared it in the header and 'regular' members don't need their type to be reiterated when they are initialised?
    – lachy
    Commented Feb 26, 2017 at 23:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.