Ric*_*iam 0 c++ compiler-construction g++ prng
我正在使用来自cygwin的g ++,我正在尝试编译一个.cpp文件,但我遇到了一个错误,
这是代码:
#include "randomc.h"
#include <time.h> // Define time()
#include <stdio.h> // Define printf()
#include <cstdlib>
int main(int argc, char *argv[] ) {
int seed = atoi(argv[1]);
int looptotal = atoi(argv[2]);
//initializes rng, I want to use argv[1] to set seed
void CRandomMother::RandomInit (int seed) {
int i;
// loop for the amount of times set by looptotal and return random number
for (i = 0; i < looptotal; i++) {
double s;
s = Random();
printf("\n%f", s)
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我在尝试使用cygwin终端和g ++进行编译时遇到的错误
Administrator@WIN-19CEL322IRP /cygdrive/c/xampp/xampp/htdocs$ g++ ar.cpp -o prog
ar.cpp: In function `int main(int, char**)':
ar.cpp:13: error: a function-definition is not allowed here before '{' token
ar.cpp:13: error: expected `,' or `;' before '{' token
Run Code Online (Sandbox Code Playgroud)
.cpp文件和头文件randomc.h位于我的xampp位置.我认为这根本不重要,不是吗?有人能告诉我如何编译并运行吗?谢谢.
将函数定义移到外面main:
//initializes rng, I want to use argv[1] to set seed
void CRandomMother::RandomInit (int seed, int looptotal) {
int i;
// loop for the amount of times set by looptotal and return random number
for (i = 0; i < looptotal; i++) {
double s;
s = Random();
printf("\n%f", s)
}
}
int main(int argc, char *argv[] ) {
int seed = atoi(argv[1]);
int looptotal = atoi(argv[2]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误消息对我来说非常清楚.
在C++中,您不允许在另一个函数中定义函数.