2012-02-29 225 views
0

我使用G ++ cygwin的版本,我試圖編譯一個.cpp文件,但我遇到了錯誤編譯C++文件,錯誤使用G ++

這裏是代碼:

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

[email protected] /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位置。我認爲這不重要,應該嗎?有人能告訴我如何讓這個編譯和運行嗎?謝謝。

+0

爲什麼你想定義'CRandomMother ::'main'內RandomInit'? – 2012-02-29 16:11:51

+2

在嘗試任何更復雜的事情之前,您需要真正學習基本的C++語法。 – Naveen 2012-02-29 16:13:08

+0

謝謝,今天過後我將學習更多關於C++的知識。我猜是因爲我開始使用PHP,我不習慣輕易改變。 – 2012-02-29 16:21:18

回答

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++中你不能定義另一個函數內部功能。

+0

+1從我,但你需要能夠看到從RandomInit looptotal,也許還呼籲RandomInit;) – wreckgar23 2012-02-29 16:13:34

+1

當然,你可能需要添加「looptotal」作爲參數傳遞給函數,和我猜測你可能也想實際叫它。 – 2012-02-29 16:15:09

+0

感謝您的快速回復! – 2012-02-29 16:18:06