2016-12-24 6 views
0

配列の範囲を指定する定数static int変数を作成します。私は問題に遭遇し、変数がクラスのメンバーではないというエラーが出ていますが、ClassName :: staticVarNameを使用してmainの変数を出力できます。配列を初期化する際にconst staticメンバを使用しようとしていません

クラスに属する静的変数を正しく設定して、配列の初期化に使用できるようにする方法がわかりません。変数はmainに出力されますが、何らかの理由でクラスの配列フィールドの範囲を定義するために変数を使用しようとするとコンパイルされません。

error: class "RisingSunPuzzle" has no member "rows"

error: class "RisingSunPuzzle" has no member "cols"

クラスのヘッダファイル:

#pragma once 
#include<map> 
#include<string> 
#include<memory> 


class RisingSunPuzzle 
{ 
private: 
    bool board[RisingSunPuzzle::rows][RisingSunPuzzle::cols]; 

public: 
    RisingSunPuzzle(); 
    ~RisingSunPuzzle(); 
    static const int cols; 
    static const int rows; 

    void solvePuzzle(); 
    void clearboard(); 
}; 

クラスのcppのファイル:

#include "RisingSunPuzzle.h" 

const int RisingSunPuzzle::cols = 5; 
const int RisingSunPuzzle::rows = 4; 


RisingSunPuzzle::RisingSunPuzzle() 
{ 
} 


RisingSunPuzzle::~RisingSunPuzzle() 
{ 
} 

void RisingSunPuzzle::solvePuzzle() 
{ 

} 

void RisingSunPuzzle::clearboard() 
{ 

} 

答えて

4

と呼ばれているデータメンバーの名前は、それらを参照するデータメンバーの前に宣言する必要がありますに。

また、静的定数を初期化する必要があります。

あなたは、彼らがODR使用されていない場合は、定数を定義する必要はありません...

クラスに次のよう

class RisingSunPuzzle 
{ 
public: 
    static const int cols = 5; 
    static const int rows = 4; 

private: 
    bool board[RisingSunPuzzle::rows][RisingSunPuzzle::cols]; 

public: 
    RisingSunPuzzle(); 
    ~RisingSunPuzzle(); 

    void solvePuzzle(); 
    void clearboard(); 
}; 

//を再フォーマットすることができます。 `彼らはスコープ内にあるよう:それでもあなたは通常1プライベート` `後RisingSunPuzzle ::`スコープrowsとcolsには追加されません

const int RisingSunPuzzle::cols; 
    const int RisingSunPuzzle::rows; 
+0

のように(初期化子なし)それらを定義することができます。 – doug

関連する問題