我应该将函数randint调用到我的main函数,但由于某种原因它不起作用。我有3个文件:randint.cpp,randint.h和main.cpp。我应该从randint.cpp调用该函数。我不确定我是否应该在头文件中声明该函数并在cpp文件中写入定义。
的main.cpp
#include "std_lab_facilities_5.h"
#include "randint.cpp"
#include "randint.h"
int main()
try {
int x = randint();
cout << x;
return 0;
}
catch (exception& e) {
cerr << "error: " <<e.what() << '\n';
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!\n";
return 2;
}
randint.cpp
#include "randint.h"
#include <chrono>
using namespace std::chrono;
//linear congruential pseudorandom number generator
int randint() {
//use the clock for an initial pseudorandom number
static long x = time_point_cast<microseconds>(system_clock::now()).time_since_epoch().count();
//calculate the next pseudorandom number
// parameters from glibc(?)
x = (((1103515245L * int (x)) & 0x7fffffff) + 12345)& 0x7fffffff;
return x;
}
randint.h
int randint();
答案 0 :(得分:-2)
在您想要的类中的.H文件中
public:
int randint();
并且.CPP中要调用所需的函数:
randint randint; //<--- Telling the class in this case main that randint exists(second randint can be whatever name you prefer)
randint.randint(); //<--- Here you call the function
我还建议您在创建新文件时使用类向导。