0

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.

rubenvb
  • 74,642
  • 33
  • 187
  • 332

1 Answers1

8

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • +1 from me, but you need to be able to see looptotal from RandomInit, and maybe also call RandomInit ;) – wreckgar23 Feb 29 '12 at 16:13
  • 1
    Of course, you'll probably need to add "looptotal" as an argument to the function, and I'm guessing you probably also want to actually call it. – Edward Loper Feb 29 '12 at 16:15
  • But I am having a problem trying to compile your corrected code Administrator@WIN-19CEL322IRP /cygdrive/c/xampp/xampp/htdocs $ g++ ar.cpp -o prog ar.cpp: In member function `void CRandomMother::RandomInit(int)': ar.cpp:12: error: `looptotal' undeclared (first use this function) ar.cpp:12: error: (Each undeclared identifier is reported only once for each function it appears in.) ar.cpp:16: error: expected `;' before '}' token Administrator@WIN-19CEL322IRP /cygdrive/c/xampp/xampp/htdocs $ – Richard William Feb 29 '12 at 16:19
  • Ok I managed to fix that, but now I have another problem $ g++ ar.cpp -o prog /tmp/ccYvWSSD.o:ar.cpp:(.text+0x1c): undefined reference to `CRandomMother::Random()' collect2: ld returned 1 exit status Administrator@WIN-19CEL322IRP /cygdrive/c/xampp/xampp/htdocs $ is there an issue? Here is the class information in the header file – Richard William Feb 29 '12 at 16:28
  • class CRandomMother { // Encapsulate random number generator public: void RandomInit(int seed); // Initialization int IRandom(int min, int max); // Get integer random number in desired interval double Random(); // Get floating point random number uint32_t BRandom(); // Output random bits CRandomMother(int seed) { // Constructor RandomInit(seed);} protected: uint32_t x[5]; // History buffer }; – Richard William Feb 29 '12 at 16:29
  • @RichardWilliam: Have you provided implementation for CRandomMother ::Random function? – Naveen Feb 29 '12 at 16:31
  • How would I go about implementing that? I have been looking on Google :S but I still do not understand this old object oriented approach to things, any help would be appreciated. – Richard William Feb 29 '12 at 16:43
  • Can someone tell me how to use method Random();? – Richard William Feb 29 '12 at 17:36