Ошибка компиляции файла C ++ с использованием g ++ - PullRequest
0 голосов
/ 29 февраля 2012

Я использую g ++ из cygwin, я пытаюсь скомпилировать файл .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.Я не думаю, что это должно иметь значение, не так ли?Может кто-нибудь сказать мне, как я могу получить это, чтобы скомпилировать и запустить, пожалуйста?Благодарю.

1 Ответ

8 голосов
/ 29 февраля 2012

Переместите определение функции за пределы 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 ++ вам не разрешено определять функции внутри другой функции.

...