2017-11-14 6 views
0

私は、コンパイル時にエラーを与え、このコンストラクタがあります - ここエラー:時代錯誤古いスタイル基底クラス初期化子[-fpermissive]

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

noob何が起こっているのか分かりません。グーグルでは、私はたった1つのstackoverflowリンクも見つけられません。

+0

すべてを削除してください。 –

+0

また、すべてのセミコロンを通常のカンマで置き換えてください – UnholySheep

+1

似たような質問https://stackoverflow.com/questions/29422285/error-anachronistic-old-style-base-クラス初期化子 – user2807083

答えて

0

thisを使用して、コンストラクタ初期化子リストで初期化するクラスメンバーを指定することはできません。 thisを完全に取り除く。それは好みやスタイルの問題ではなく、単なる誤りです。

"古いスタイルの基本クラス"に関するエラーメッセージは構文上の混乱に過ぎません。メンバーイニシャライザのリストに無意味な構文があり、コンパイラが混乱することがあります。

+0

そして、なぜこれがダウン表示されていますか?それは完全に正しいです。 – Bathsheba

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

コンパイラは、関数パラメータと、このインスタンスのクラスメンバ変数との間に明確化することが可能である:パラメータyy(y)を初期化部材y

最後に、signedのようなタイプのメンバーをお待ちしていますか?

+0

私は 'this->'構文でコンパイルできません。エラーメッセージはそれほど役に立たない:[コンパイラエクスプローラ](https://godbolt.org/g/d2gSa2) –

+2

'this->' inコンストラクタの初期化子リストは単に違法です。 「従来の」ではありません。 – AnT

+0

@DanielH:おそらくそれも時代遅れです! (実際、私はちょうど最新のGCCで試したことがあります!)私はそれを錫メッキしました。 – Bathsheba

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

を捨て、私はそれが、この場合には、優れたエラーメッセージではないことに同意するだろう。

+0

あなたの最後の編集は不要です。 'y(y)'のスコープは問題ありません。 – rustyx

+0

コンパイラではなく、わかりやすいようにしました:) – Steve

関連する問題