在抛出 std::string 实例后终止调用
Terminate called after throwing an instance of std::string
我正在尝试确定用户实例化的变量是否存在于方程中。我有这样的声明类:
#ifndef EQUATION_H
#define EQUATION_H
#include "Expression.h"
#include "Shunt.h"
#include <string>
#include <map>
using namespace std;
class Equation
{
public:
Equation(string eq);//If an equation is invalid, throw an exception string
Equation(const Equation& other);
Equation& operator=(const Equation& other);
~Equation();
int evaluateRHS();//If at least one variable does not have a value, throw an exception string
int evaluateLHS();//If at least one variable does not have a value, throw an exception string
void instantiateVariable(char name, int value);//If does not exist in the equation, throw an exception string
int RHSdistanceFromLHS();//If at least one variable does not have a value, throw an exception string
string original;
map<char, int> variables;
private:
Expression* left;//Left side of the equals
Expression* right;//Right side of the equals
};
#endif
并在下面实现实例化变量:
void Equation::instantiateVariable(char name, int value)
{
short firstLen = left->equationPart.length();
short secLen = right->equationPart.length();
bool exists = false;
for(short i = 0; i < firstLen; i++)
{
if(left->equationPart[i] != name)
{
exists = false;
}
else
{
exists = true;
}
}
if(exists == false)
{
for(short i = 0; i < secLen; i++)
{
if(right->equationPart[i] != name)
{
exists = false;
}
else
{
exists = true;
}
}
}
if(exists == false)
{
string er = "error"; //Not caught successfully. Terminates entire program.
throw er;
}
variables[name] = value;
}
在主函数中调用时:
void testCase3()
{
try
{
Equation equation3("(y - (x + 3) ^ y) * 5 / 2 = log 20 - y");
equation3.instantiateVariable('z',3);
}
catch(string ex)
{
cout<<"Exception thrown"<<endl;
}
}
int main(int argc, char** argv)
{
testCase3();
}
发生以下错误:
terminate called after throwing an instance of 'std::string'
Aborted
请尝试捕获常量引用,
catch(const string& ex) {}
见 https://stackoverflow.com/a/2522311/1689451
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
- 在抛出 'std::invalid_argument' 的实例后终止调用 what(): stoi (找不到问题所在)
- 终止调用本机代码的 .Net 线程
- C++程序问题:抛出'std::invalid_argument'实例后终止调用
- 当可以返回错误/异常时,从库中终止调用程序(例如,调用exit())是否总是错误的?
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc
- 在抛出 'std::bad_weak_ptr' 的实例后终止调用 what(): bad_weak_ptr?
- 调试"在抛出 ..) 实例后终止调用",当异常 _should_ 被捕获时
- 引发实例后终止调用 - C++错误
- 在抛出 std::bad_alloc 实例后终止调用.使用两个类,一个带有指向另一个的指针
- 在抛出"pqxx::syntax_error"的实例后终止调用?
- C++11 快速排序终止调用,没有活动异常
- C++:在没有活动异常(GCC)的情况下终止调用
- 在抛出 std::string 实例后终止调用
- 在抛出"std::bad_alloc"实例后终止调用
- 在抛出"std::out_of_range"实例后终止调用