0

I have this constructor which give an error on compilation :-

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);   {};

here is full error:-

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);  {};
                 ^

As a noob I have no idea what is going on. On googling ,I only find one stackoverflow link which also doesn't help.

3 Answers3

3

The way you have written it is not standard C++, hence the compiler diagnostic. Rewrite to the conventional syntax:

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
{
}

The compiler is able to disambiguate between a function parameter and a class member variable in this instance: y(y) initialises member y with parameter y.

Finally, are you sure you want signed types for time-like members?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

When initialising using an initializer list, you need to seperate it by ,, not ;. And ditch the 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)
{
}

I would agree that it's not a great error message, in this case.

Steve
  • 1,747
  • 10
  • 18
0

You are not allowed to use this to specify the class member you want to initialize in the constructor initializer list. Get rid of this entirely. It is not a matter of preference or style, it is just an error.

The error message about "old-style base class" is just a syntactic confusion: seeing nonsensical syntax in member initializer list the compiler gets confused.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765