C++ - Noob - 简单的尝试/捕获

Jon*_*Jon 0 c++ exception-handling

我正在尝试编写一个简单的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";
    } 
}
Run Code Online (Sandbox Code Playgroud)

我收到以下编译器错误:

LNK2019:未解析的外部符号"public:int_ thiscall Exception :: divide(int,int"(?divide @ Exception @@ QAEHH @ Z)在函数"public: _thiscall Exception :: Exception(void)"中引用(?? 0Exception @ @ QAE @ XZ)

LNK1120:1个未解析的外部

Set*_*gie 5

我神奇的远程调试技能告诉我它divide是一个成员Exception,但你在全局命名空间中定义它.前缀divideException::,一个la

int Exception::divide(int x, int y)
{
    if (y == 0) {
        throw 0;
    }
    return x / y;
}
Run Code Online (Sandbox Code Playgroud)