2009-11-24 57 views
0

我有以下代碼:的Int32在此範圍內沒有聲明

//Comp454 program 2 
#include <iostream> 
#include <string> 
#include <fstream> // file I/O support 
#include <cstdlib> // support for exit() 
const int SIZE = 60; 

int main() 
{ 
using namespace std; 
string states; 
int numStates = 0, i = 0, stateVar = 0; 
string line; 
char filename[SIZE]; 
ifstream inFSM, inString; 

//Open FSM definition 
cout << "Enter name of FSM definition: "; 
cin.getline(filename, SIZE); 
inFSM.open(filename); //Associate inFile with a file 
if (!inFSM.is_open()) // failed to open file 
{ 
cout << "Could not open the file " << filename << endl; 
cout << "Program terminating.\n"; 
exit(EXIT_FAILURE); 
} 

//Process FSM definition line by line until EOF 
getline(inFSM, states); 
numStates = Int32.TryParse(states); 

//Check for num of states 
if(numStates > 10) 
{ 
    cout << "There can be no more than 10 states in the FSM definition, exiting now." << endl; 
    return 0; 
} 

while (!inFSM.eof()) // while input good and not at EOF 
{ 
    getline(inFSM, line); 
    cout << line << endl; 
} 

return 0; 
} 

我試圖將字符串轉換爲使用Int32.TryParse()的整數,但我編譯時得到錯誤的Int32在這方面沒有申明。不知道爲什麼會出現這種情況,我是否缺少名稱空間聲明?任何幫助表示讚賞,謝謝

更新: 感謝所有的答覆,如在我發佈的評論中,我沒有嘗試使用C++/CLI,我如何轉換字符串,從字符串類聲明,到一個整數?

回答

3

嘗試使用atoi()代替。美國是一個std ::字符串,所以你需要說:

numStates = atoi(states.c_str()); 
+1

我寧願建議'strtol',如'atoi'沒有錯誤報告機制(你不知道你是否已經解析一個等於零的數字,或者如果你沒有解析一個不是數字的字符串 - 在這兩種情況下,你都會從'atoi'得到'0')。 – 2009-11-24 04:19:37

1

看起來您正在編譯使用.NET Int32類的直C++應用程序來解析值。

如果您確實正在編譯.NET應用程序,或者使用像atoi()這樣的函數來解析字符串值,則需要引用System命名空間和CLR支持。