gpt4 book ai didi

c++ - 非常简单的代码,出现错误 C2712,不明白为什么

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:27:22 26 4
gpt4 key购买 nike

error C2712: Cannot use __try in functions that require object unwinding 我遇到了一段时间的麻烦,在缩小问题范围后,我只剩下一个非常非常简单的代码,我可以不明白为什么会导致这个错误。我在 Windows 下使用 Visual Studio。

我正在使用/EHa 进行编译(我不使用/EHsc)

我使用 __try/__except 而不是 try/catch 的原因是因为我想捕获所有错误,并且不希望程序在任何情况下崩溃,包括例如除以 0,try-catch 不会捕获。

#include <string>
static struct myStruct
{
static std::string foo() {return "abc";}
};

int main ()
{
myStruct::foo();

__try
{ }
__except (true)
{ }

return 0;
}

输出:

error C2712: Cannot use __try in functions that require object unwinding

最佳答案

这是解决方案。有关详细信息,请阅读 Compiler Error C2712

#include <string>
struct myStruct
{
static std::string foo() {return "abc";}
};

void koo()
{
__try
{ }
__except (true)
{ }
}

int main ()
{
myStruct::foo();
koo();
return 0;
}

额外注意:如果没有声明使用您的结构 (myStruct),则不需要 static

关于c++ - 非常简单的代码,出现错误 C2712,不明白为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24748384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com