2012-10-23 34 views
1
#include <iostream> 
#include <fstream> 
#include <stdio.h> 
#include <math.h> 

我需要此代碼的幫助。我的編譯器不斷要求我使用-fpermissive選項,但我不知道在哪裏輸入它。我粘貼了下面的代碼和顯示的錯誤。- 代碼塊中的最佳選項。我在哪裏可以放入「-foumissive」選項?

using namespace std; 

int cx = 0, cy = 0; 

double x =0, y=0, r = 5; 


int main(){ 

ofstream myfile; 

myfile.open("dingo.txt"); 

for(int g =0 ; g <= 360; g++) 

//find x and y 

//Get points of a circle. 

x = cx + r * cos(g); 

y = cy + r * sin(g); 

//This is what pops up below: 

//In function int main()': 

//Error: name lookup of 'g' changed for ISO 'for' scoping [-fpermissive] 

//note: (if you use '-fpermissive' G++ will accept your code) 

//where should I enter the required option? 

myfile << "X: " << x << "; Y: " << y <<endl; 

myfile.close(); 

return 0; 

} 
+9

您不應該使用'-fpermissive',而應該修復代碼中的錯誤。 –

+1

爲什麼你想使用'-fpermissive'而不是修復你的代碼? –

+3

即使你使用'-fpermissive',代碼也不會做你想要的。 – hvd

回答

4

您可以在添加更多的編譯器標誌"Other Options""Settings">"Compiler"

enter image description here

雖然我覺得你應該先解決您的代碼。例如,std::sinstd::cos接受弧度,而不是度數。您還需要在您的for聲明中使用大括號。

for(int g =0 ; g <= 360; g++) { 
    //code here. 
} 
+0

特別感謝弧度部分。我從來沒有知道這一點。 – ARTHUR

1

不要使用-fpermissive

這意味着「我真的,真的知道我在這裏做什麼,所以請閉嘴」,永遠不會是一個很好的初學者的選擇。

在這種情況下,「g ++會接受你的代碼」的意思是「g ++不會抱怨你的代碼,但錯誤仍然存​​在,你會浪​​費很多時間尋找它們,因爲代碼編譯沒有那麼多作爲警告「。

縮進你的代碼正確暴露出問題:

int main(){ 
    int cx = 0, cy = 0; 
    double x = 0, y = 0, r = 5; 
    ofstream myfile; 
    myfile.open("dingo.txt"); 
    for(int g = 0 ; g <= 360; g++) 
     x = cx + r * cos(g); 
    y = cy + r * sin(g); // <--- Here it is. 
    myfile << "X: " << x << "; Y: " << y <<endl; 
    myfile.close(); 
    return 0; 
} 

很明顯,指示行使用g,這是循環變量。
在過去,在for -loop中聲明的變量的作用域實際上是封閉循環的作用域(在你的情況下爲main函數)。
這個後來改變了,所以循環變量的作用域被限制在內部的,但由於依賴於舊規則的遺留代碼很多,編譯器提供了一種啓用過時行爲的方法。

您打算什麼大概是這樣的:

for(int g = 0; g <= 360; g++) 
{ 
    x = cx + r * cos(g); 
    y = cy + r * sin(g); 
    myfile << "X: " << x << "; Y: " << y <<endl; 
} 

(這是錯誤的,因爲sincos使用弧度,不度 - 但我會離開這個問題作爲練習。)

相關問題