runtime_error未定义!怎么解决?

哈利_蜘蛛侠 2013-10-06 04:18:42
按照《C++ Primer》上面的例子,在VS 2008里进行编译,可是编译的时候提示错误,就是
runtime_error identifier not found
这个是什么情况?

//
#include <iostream>
#include <stdexcept>
using std::cin;
using std::cout;
using std::endl;
void keep_window_open();

int main()
{
int dividend=1, divisor=1;
cout<<"\nInput the dividend:\t";
cin>>dividend;

cout<<"\nInput the divisor:\t";
cin>>divisor;
if(divisor <=0)
throw runtime_error("\nNo! The divisor must be a positive number!\n");

keep_window_open();
}

void keep_window_open()
{
cout << "\nPress any key to exit:";
getchar();
}


难道是编译器的问题?不会这么坑吧?
...全文
682 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
booirror 2013-10-07
  • 打赏
  • 举报
回复
用std::runtime_error 替换runtime_error
哈利_蜘蛛侠 2013-10-07
  • 打赏
  • 举报
回复
哈哈哈,终于搞定啦!确实如4楼所言哦!下面是我编的一个无聊的小程序:

//
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::runtime_error;

void keep_window_open();

int main()
{
	int dividend=1, divisor=1;
	int result=1;
	char decide1 = 'y', decide2 = 'y';
	
	while(decide1 == 'y')
	{
		cout<<"\nInput the dividend:\t";
		cin>>dividend;
		try
		{
			if(dividend<0)
			{
				throw runtime_error("For convenience, please make sure dividend is non-negative!\n");

			}
			decide1 = 'n';
		
			while(decide2 == 'y')
			{
				cout<<"\nInput the divisor:\t";
				cin>>divisor;
				try
				{
					if(divisor<=0)
						throw runtime_error("For convenience, divisor must be positive!\n");
					decide2 = 'n';
					result = dividend/divisor;
				}catch(runtime_error err)
				{
					cout << err.what()
						 << "\n Try Once More? Enter y or n "<<endl;
					cin >>decide2;
				}
			}
		}catch(runtime_error err)
			{
				cout << err.what()
					 << "\n Try Again? Enter y or n" << endl;
				cin >>decide1;
			}
	}

	cout <<"\nSo the quotient of "<<dividend<<" divided by "<<divisor<<" is:\t"<<result<<endl;

	keep_window_open();
}

void keep_window_open()
{
	cout << "\nPress any key to exit:";
	getchar();
}
stereoMatching 2013-10-07
  • 打赏
  • 举报
回复
引用 3 楼 u011873969 的回复:
[quote=引用 1 楼 stereoMatching 的回复:]
#include<cstdio>
另外,你可以用

std::cin.get();
取代

getchar();
不行哦,报错还是一样的。[/quote] 没注意到你有throw 加个try{}catch{}吧
SmallCoder1992 2013-10-06
  • 打赏
  • 举报
回复

你这个是有问题的,你在抛出异常之后不进行处理吗??你需要进行try - catch,不然程序会出现无法对throw进行处理的
stereoMatching 2013-10-06
  • 打赏
  • 举报
回复
#include<cstdio>
另外,你可以用

std::cin.get();
取代

getchar();
FancyMouse 2013-10-06
  • 打赏
  • 举报
回复
std::runtime_error
哈利_蜘蛛侠 2013-10-06
  • 打赏
  • 举报
回复
引用 1 楼 stereoMatching 的回复:
#include<cstdio>
另外,你可以用

std::cin.get();
取代

getchar();
不行哦,报错还是一样的。

65,144

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧