【发布时间】:2015-06-29 14:02:29
【问题描述】:
我在尝试将数字转换为字符串时遇到问题。目的是进行错误检查以确保数字具有特定长度。我曾尝试同时使用 to_string() 和 convert.str() 函数,但在尝试编译时得到相同的错误。
我正在使用 MinGw g++ 进行编译并意识到我需要告诉它我想要 C++11 标准,我相信我已经做到了。我的编译器代码如下:
NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW\bin\g++ -std=c++11 "$(FULL_CURRENT_PATH)" -o "$(NAME_PART).exe"
cmd /c $(NAME_PART).exe
现在假设这是正确的,我使用to_string() 的代码如下:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
int book_code = 0;
cout << "Please enter the four digit book code: ";
cin >> book_code;
string code = to_string(book_code);
while (!(cin >> book_code) || code.length() != 4){
cin.clear();
cin.ignore(10000, '\n');
cout << "That is not a valid code." << endl;
cout << "Please enter the four digit book code: ";
}
}
而我使用 convert.str() 的代码如下:
int main() {
int book_code = 0;
cout << "Please enter the four digit book code: ";
cin >> book_code;
ostringstream Convert;
convert << book_code;
string code = convert.str();
while (!(cin >> book_code) || code.length() != 4){
cin.clear();
cin.ignore(10000, '\n');
cout << "That is not a valid code." << endl;
cout << "Please enter the four digit book code: ";
}
}
这些都没有成功,都返回了
错误:'to_string' 未在此范围内声明
我是否遗漏了一些明显的东西?
【问题讨论】:
-
我敢打赌这是这个问题的重复:stackoverflow.com/questions/15569179/…
-
似乎可以编译(并运行); coliru.stacked-crooked.com/a/01ee396b5116e4a3
-
我 90% 确定这是 mingw 旧版本的问题。尝试 mingw-w64,该项目实际上已更新(尽管名称支持 32 位二进制文件)
-
Jean,我按照该帖子中的说明进行操作,不幸的是我收到了错误消息:首先它说 g++.exe 已停止响应。我单击取消,然后我收到应用程序错误。 "应用程序无法正确启动 (0xc0000005)。