C++ - Noob - Simple Try/Catch
C++ - Noob - Simple Try/Catch
我正试图编写一个简单的try/catch语句,但我不断收到编译器错误。这是我的代码:
int divide(int x, int y)
{
if (y == 0) {
throw 0;
}
return x / y;
}
Exception::Exception()
{
try {
cout << divide(10, 0) << "n";
} catch (int e) {
cout << "Cannot divide by " << e << "n";
}
}
我得到以下编译器错误:
LNK2019:未解析的外部符号"public:int_thiscall异常::divide(int,int)"(?divide@Exception@@QAEHH@Z)在函数"public:_thiscall Exception::Exception(void)"中引用(??0Exception@@QAE@XZ)
LNK1120:1个未解析的外部
我神奇的远程调试技能告诉我divide
是Exception
的成员,但您在全局命名空间中定义它。在divide
前面加上Exception::
,即
int Exception::divide(int x, int y)
{
if (y == 0) {
throw 0;
}
return x / y;
}
您得到的是一个链接器错误。您提到divide
是Exception
类的一个成员函数,但忘记了实现它。只需使用::
确认通话即可。
cout << ::divide(10, 0) << "n"; // Take the function at global scope
// This still leaves the member function implemenation
// unimplemented which is bad though.
或
int Exception :: divide(int x, int y) { .... }
相关文章:
- 编译器是否必须始终删除 try-catch 块(如果它被证明是非抛出的)
- 有没有更好的方法来处理异常? try-catch块真的很丑
- 我可以使用 try catch 语句来捕获任何错误而不是具体错误吗?
- 在大型应用程序的main上使用try-catch
- RapidXML 节点在 try catch 块中具有正确的值,但它在块外为 nullptr
- 提取 try-catch 时出现运行时错误
- std::unique_ptr 在 try-catch 块中未捕获取消引用异常
- 何时删除 try-catch 块中的指针
- 是否有理由大多数/所有 try-catch 示例只对 throw 语句使用 void 子函数
- 循环try..catch ..确保输入有效或使用控制语句
- 构造函数中的异常:init() 方法、指针、大型 try/catch 或
- C++ C2509 在讲师的定义中使用 try: - catch
- 为什么未达到的 try-catch 块会增加运行时时间
- 在 C++ 中使用 try-catch 进行删除 [] 或删除操作是否必要
- ex.nested try-catch中的什么()更改
- 为什么 c++ try-catch 块不能在 ubuntu 上与 g++ 一起使用?
- 格式化文件异常被 try catch 块忽略
- libpng错误不会被try/catch块捕获
- 在 C++ 和 C# 中使用 try/catch 的正确方法是什么?
- 如何使用catch try-catch