5

这是一个程序:

#include <iostream>
using namespace std;

int main() {
cout << "Enter a number";
int i;
cin >> i;
try {
    if( i == 0 ) throw 0;
    if( i == 2 ) throw "error";
} catch( int i ) {
    cout << "can't divide by 0";
 }
   catch( ... ) {
       cout << "catching other exceptions";
   }
}

在编译(Windows 7 上的 Microsoft Visual C++ 2010 Express)时,我收到错误消息:

致命错误 LNK1169:找到一个或多个多重定义的符号

4

4 回答 4

16

Actually there is no error in this code.

Number of source files could be the problem. Try this code as a new project in the same compiler or try deleting the files from the source files option in the left side of Text Area (i.e where you are writing your code)

This should compile then.

于 2011-06-28T12:42:12.697 回答
5

最后,我认为我找到了最合理的问题解释,因为您知道我们通常在 .cpp 文件中将 main 分配为整数(int main),有时我们可能会在同一个项目中编写多个 .cpp 文件(int main () ).so 对于程序,这意味着我们不小心在同一个项目文件夹中重复了两次相同的功能。我们要做的是这只是用 (int main) 编写一个 .cpp 文件,而另一个 .cpp同一个项目中的文件用 (int submain) 写入它们,看看会发生什么。

于 2012-11-21T08:30:09.577 回答
2

尝试将您的更改int main()int submain().

于 2012-02-25T10:36:10.200 回答
-4

我怀疑你的错误来自这一行:

catch(int i)

您已经在此范围内获得了一个类似名称的变量。此外,您应该捕获异常,而不是整数。

于 2011-06-28T12:30:53.083 回答