我有以下代码:
//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,如何将字符串类(从字符串类声明)转换为整数?
最佳答案
尝试使用 atoi()反而。 States 是一个 std::string 所以你需要说:
numStates = atoi( states.c_str() );
关于c++ - Int32 未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1787599/