2013-08-23 71 views
0

我正在嘗試從文件中讀取一個「數字」字符串,並將其轉換回整數。以下是我的代碼,這是C++/CLI在使用C++/CLI示例時編譯時間錯誤

int InformationReader::getThreshold() 
{ 
    StreamReader ^reader = gcnew StreamReader("threshold.dat"); 
    System::String ^thresholdStr = reader->ReadLine(); 

    Int32 thresholdNum; 

    boolean a = Int32::TryParse(thresholdStr,thresholdNum); 

    return 0; 

} 

但是,一旦該代碼得到執行,我收到以下錯誤

1>InformationReader.cpp(29): error C2065: 'Int32' : undeclared identifier 
1>InformationReader.cpp(29): error C2146: syntax error : missing ';' before identifier 'thresholdNum' 
1>InformationReader.cpp(29): error C2065: 'thresholdNum' : undeclared identifier 
1>InformationReader.cpp(31): error C2065: 'boolean' : undeclared identifier 
1>InformationReader.cpp(31): error C2146: syntax error : missing ';' before identifier 'a' 
1>InformationReader.cpp(31): error C2065: 'a' : undeclared identifier 
1>InformationReader.cpp(31): error C2653: 'Int32' : is not a class or namespace name 
1>InformationReader.cpp(31): error C2065: 'thresholdNum' : undeclared identifier 
1>InformationReader.cpp(31): error C3861: 'TryParse': identifier not found 

現在好了,這是外星人給我,因爲我去通過一些問題和答案,並在所有這些問題上他們都遵循了我曾經使用過的類似方法,但是我得到一個錯誤。爲什麼是這樣?請幫忙。

+0

前我不是C++專家,所以想知道你是怎麼在C++中指定了參數,像在C#示例'Int32.TryParse (「2」,out i);'。只是好奇導致TryParse的整個目的不是拋出異常。 – Nilesh

+0

你陷入了編譯片段的陷阱,沒有他們的線束。 – user7116

+0

缺少常用的'using namespace System;'指令。布爾應該大寫或是bool。 –

回答

2

修復首先看到的編譯錯誤:您需要一個System::中的Int32 thresholdNum;

+2

或者,你可以簡單地在兩個地方使用'int'來代替'Int32'(是的,'int :: TryParse'也可以)。 – Medinoc

+0

非常感謝您的幫助。 –

+0

@Medinoc:是的,我現在做了。非常感謝您的幫助 –