错误:不合时宜的旧式基类初始值设定项[-fpermissive]

时间:2017-11-14 15:31:42

标签: c++ error-handling

我有这个构造函数,它在编译时出错: -

Time::Time(short y,short m,short d,short h,short mi,short s):
        (*this).y(y);
        (*this).m(m);
        (*this).d(d);
        (*this).h(h);
        (*this).mi(mi);
        (*this).s(s);   {};

这里是完整的错误: -

Time.cpp: In constructor ‘Time::Time(short int, short int, short int, short int, short int, short int)’:
Time.cpp:22:2: error: anachronistic old-style base class initializer [-fpermissive]
  (*this).y(y);
  ^
Time.cpp:21:61: error: unnamed initializer for ‘Time’, which has no base classes
 Time::Time(short y,short m,short d,short h,short mi,short s):
                                                             ^
Time.cpp:22:9: error: expected ‘{’ before ‘.’ token
  (*this).y(y);
         ^
Time.cpp: At global scope:
Time.cpp:22:9: error: expected unqualified-id before ‘.’ token
Time.cpp:23:4: error: expected unqualified-id before ‘this’
  (*this).m(m);
    ^~~~
Time.cpp:23:4: error: expected ‘)’ before ‘this’
Time.cpp:24:4: error: expected unqualified-id before ‘this’
  (*this).d(d);
    ^~~~
Time.cpp:24:4: error: expected ‘)’ before ‘this’
Time.cpp:25:4: error: expected unqualified-id before ‘this’
  (*this).h(h);
    ^~~~
Time.cpp:25:4: error: expected ‘)’ before ‘this’
Time.cpp:26:4: error: expected unqualified-id before ‘this’
  (*this).mi(mi);
    ^~~~
Time.cpp:26:4: error: expected ‘)’ before ‘this’
Time.cpp:27:4: error: expected unqualified-id before ‘this’
  (*this).s(s);  {};
    ^~~~
Time.cpp:27:4: error: expected ‘)’ before ‘this’
Time.cpp:27:17: error: expected unqualified-id before ‘{’ token
  (*this).s(s);  {};
                 ^

作为一个菜鸟我不知道发生了什么。在谷歌搜索,我只找到一个stackoverflow链接,也没有帮助。

3 个答案:

答案 0 :(得分:3)

您编写它的方式不是标准C ++,因此编译器诊断。重写为传统语法:

Time::Time(short y,short m,short d,short h,short mi,short s):
    y(y),
    m(m),
    // and so on, without a final comma
{
}

在此实例中,编译器能够消除函数参数和类成员变量之间的歧义:y(y)使用参数y初始化成员y

最后,您确定要为类似时间的成员提供signed类型吗?

答案 1 :(得分:3)

使用初始值设定项列表进行初始化时,您需要按,分隔,而不是;。抛弃this

Time::Time(short _y,short _m,short _d,short _h,short _mi,short _s):
    y(_y),
    m(_m),
    d(_d),
    h(_h),
    mi(_mi),
    s(_s)
{
}

我同意在这种情况下,它不是一个很好的错误信息。

答案 2 :(得分:0)

不允许您使用<td-layout> <td-navigation-drawer flex [sidenavTitle]="name" logo="assets:covalent"> Top level navigation drawer </td-navigation-drawer> <td-layout-nav [toolbarTitle]="(media.registerQuery('gt-xs') | async) ? 'Dashboard' : ''" logo="assets:covalent" navigationRoute="/"> <button mat-icon-button td-menu-button tdLayoutToggle> <mat-icon>menu</mat-icon> </button> <div td-toolbar-content> Top toolbar </div> <td-layout-manage-list #manageList> <div td-sidenav-content layout="column" layout-fill> Inner sidenav listing dashboards </div> <mat-sidenav-container flex> <mat-sidenav #sidenav align="end"> Right Sidenav </mat-sidenav> <td-layout-nav> <div td-toolbar-content> Second toolbar </div> <div flex layout-gt-sm="row"> <div flex-gt-sm="50"> <mat-card> ... </mat-card> </div> </div> </td-layout-nav> </mat-sidenav-container> </td-layout-manage-list> <td-layout-footer>Footer content</td-layout-footer> </td-layout-nav> </td-layout> 指定要在构造函数初始值设定项列表中初始化的类成员。完全摆脱this。这不是偏好或风格问题,只是一个错误。

关于“旧式基类”的错误消息只是语法上的混淆:在成员初始化列表中看到无意义的语法,编译器会感到困惑。