0
我想創建一個常量靜態int變量來指定數組的範圍。我遇到了問題,並得到錯誤說變量不是該類的成員,但我可以使用ClassName :: staticVarName打印出主變量。嘗試使用它初始化數組時,未標識C++ const靜態成員
我無法弄清楚如何正確設置一個屬於某個類的靜態變量,以便它可以用來初始化一個數組。該變量在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()
{
}
定義它們(不初始化)正常情況下一個不經過''私人:: RisingSunPuzzle'添加範圍行和cols:'因爲他們在範圍之內。 – doug