0

我可以在一个简单的应用程序中获得 mt19937 rng 种子。现在,我试图为每个应用程序播种一次,并在需要时多次使用它。这是我的代码。我得到的错误在 GenerateRandomNumber - “gen: undeclared identifier”中。

主文件

#include <iostream>
#include "general.h"

int main() {

CallOncePerApp();

// Loop for output testing
// Ultimately, GenerateRandomNumber can be called from anywhere in any CPP file
for (int i=0;i<10;i++)  {
    int dwGen = GenerateRandomNumber(1, 1000);
    cout << dwGen; // print the raw output of the generator.
    cout << endl;
    }
}

一般.h:

#include <random>

using namespace std;

extern random_device rd;
extern void CallOncePerApp();
extern mt19937 gen;

extern unsigned int GenerateRandomNumber(unsigned int dwMin, unsigned int dwMax);

一般.cpp:

#include <random>

using namespace std;
random_device rd;
mt19937 gen;

void CallOncePerApp() 
{
    // Error C2064 term does not evaluate to a function taking 1 arguments
    gen(rd); // Perform the seed once per app
}

unsigned int GenerateRandomNumber(unsigned int dwMin, unsigned int dwMax) 
{
    uniform_int_distribution <int> dist(dwMin, dwMax); // distribute results between dwMin and dwMax inclusive.
    return dist(gen);
}
4

1 回答 1

0

这里,

mt19937 gen; // gen is an object of class mt19937! not a function

void CallOncePerApp() 
{
    // Error C2064 term does not evaluate to a function taking 1 arguments
    gen(rd); // **** therefore this line is wrong!
}

您还需要在“general.cpp”文件中包含头文件。

于 2019-03-24T13:29:31.843 回答