0

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 の場所にあります。これはまったく問題にすべきではないと思いますよね?これをコンパイルして実行する方法を教えてください。ありがとう。

4

1 に答える 1

8

関数定義を外側に移動します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++ では、別の関数内で関数を定義することはできません。

于 2012-02-29T16:11:08.840 に答える