使用g++编译C++文件时出错

Error compiling C++ file using g++

本文关键字:出错 文件 C++ g++ 编译 使用      更新时间:2023-10-16

我正在使用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位于我的examplep位置。我认为这根本不重要,是吗?有人能告诉我如何编译并运行这个吗?谢谢

将函数定义移动到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++中,不允许在另一个函数中定义函数。