c++
class Screen{
public:
typedef string::size_type pos;
private:
pos cursor = 0;
pos height = 0, width = 0;
string contents;
public:
Srceen() = default; //这里显示 Error:缺少显式类型(假定"int") 为什么?
Screen(pos ht, pos wd) : height(ht), width(wd), contents(ht*wd, ' '){}
Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht*wd, c){}
};
我还只是在定义过程,为什么会出这类错误
版本:VS2013
定义构造函数只可能是带大括号的形式,怎么会有等号形式。
调用构造函数赋值给变量时才用等号。
class Screen
{
public:
typedef string::size_type pos;
private:
pos cursor = 0;
pos height = 0, width = 0;
string contents;
public:
Srceen(); //这里显示 Error:缺少显式类型(假定"int") 为什么?
Screen(pos ht, pos wd) : height(ht), width(wd), contents(ht*wd, ' '){}
Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht*wd, c){}
};
Srceen() = default; //这样的写法,那位大神教的???
《C++Primer》(第五版中文版) P235 构造函数
我开始学的时候也遇见过,这里你使用default,只是表示声明为默认的,但是你需要定义一下这个构造函数