【问题标题】:Default Constructor Compilation error expected unqualified id默认构造函数编译错误预期不合格 id
【发布时间】: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**
}

【问题讨论】:

  • trycatch 是关键字,而不是名称,当然也不是命名空间 std 的成员。把std::放在他们面前。
  • 我删除了命名空间标签 std:: 因为 try 和 catch 是关键字,但我仍然得到同样的错误:

标签: c++ constructor compilation compiler-errors try-catch


【解决方案1】:

编译器认为{ try ... }terrainbrace-init-list。如果您打算默认构造它,请这样做:

: terrain{}
{
    ...

或者这个:

: terrain()
{
    ...

如果terrainclass 的实例,则省略整个成员初始化列表

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    相关资源
    最近更新 更多