【发布时间】:2016-04-17 14:04:41
【问题描述】:
我想使用方法 loadfile 从 File 构建一个 Default Constructure 来初始化我的属性(这是一个实例)。如果无法加载文件,我还希望引发异常,如果是这种情况,我需要调用我的 reset() 函数。
对于以下代码,在尝试编译时,我总是在 try 行遇到相同的错误。
Env.cpp:16:8: error: 'try' 之前的预期 unqualified-id
Env::Env() **// default constructor of Env class**
: terrain **// terrain is an instance of a class I declared in hpp file**
{
try
{Env::loadFile();} **// the method loadfile throws an error it failed**
catch(std::runtime_error)
{std::cerr << " Error " << endl;
Env::reset();} **// calls reset function if file loading failed**
}
【问题讨论】:
-
try和catch是关键字,而不是名称,当然也不是命名空间std的成员。把std::放在他们面前。 -
我删除了命名空间标签 std:: 因为 try 和 catch 是关键字,但我仍然得到同样的错误:
标签: c++ constructor compilation compiler-errors try-catch