简体   繁体   中英

Error compiling C++ file using g++

I am using g++ from cygwin, I am trying to compile a .cpp file but I am encountering an error,

here is the code:

#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;

}

Here is the error I am getting when trying to compile using cygwin terminal and 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

the .cpp file, and the header file randomc.h, are located in my xampp location. I don't think this should matter at all, should it? Can someone tell me how I can get this to compile and run please? Thanks.

Move the function definition outside 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;
}

The error message seems pretty clear to me.

In C++ you're not allowed to define functions inside another function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM