我正在使用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;
}以下是我在尝试使用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.cpp文件和头文件randomc.h位于我的xampp位置。我认为这一点都不重要,不是吗?有人能告诉我怎样才能编译并运行这个程序吗?谢谢。
发布于 2012-03-01 00:11:09
将函数定义移到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;
}错误消息对我来说似乎很清楚。
在C++中,不允许在另一个函数中定义函数。
https://stackoverflow.com/questions/9502584
复制相似问题