我的新类(class)名为Fountainofyouth遇到了问题。尝试构建整个项目后,调试器显示

warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]


错误:expected '{' before 'Fountainofyouth'有人可以告诉我怎么回事吗?
Fountainofyouth.cpp的内容:
#include "Fountainofyouth.h"

//warning
Fountainofyouth::Fountainofyouth(int startDrinks) : Field
{
    //ctor
}

//error
Fountainofyouth::~Fountainofyouth()
{
    //dtor
}

string Fountainofyouth::fieldType()
{
    return 0;
}

bool Fountainofyouth::canEnter(Unit* unit)
{
    return 0;
}

void Fountainofyouth::affect(Unit* unit)
{

}

`

最佳答案

构造函数定义不应包含: Field部分:

Fountainofyouth::Fountainofyouth(int startDrinks) // : Field <- remove this
{
    //ctor
}

如果要从Fountainofyouth继承Field,则在类的定义中而不是在其构造函数上进行继承。
struct Fountainofyouth : Field
{
    // declaration of ctor, dtor, etc.
};

或者,如果FieldFountainofyouth的成员,则在value initialize上添加一对括号:
Fountainofyouth::Fountainofyouth(int startDrinks) : Field()
{
}

10-08 13:23