当尝试使用它来初始化数组时,未识别C const静态成员
C++ const static member is not identified when trying to use it to initialize an array
我想创建一个常数静态int变量来指定数组的范围。我遇到问题并遇到错误,说该变量不是类的成员,但是我可以使用className :: staticvarname在main中打印出变量。
我无法弄清楚如何正确设置属于类的静态变量,以便可以将其用于初始化数组。变量打印在MAIN中,但由于某种原因,当我尝试使用它来定义类的数组范围时,它不会编译。
错误:class'risingsunpuzzle'没有成员"行"
错误:class'risingsunpuzzle'没有成员" 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()
{
}
必须在将其推荐给它们的数据成员之前声明所引用的数据成员的名称。
还必须初始化静态常数。
您可以按照以下方式重新格式化
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();
};
//...
如果不使用ODR,则无需定义常数。但是,您可以定义它们(没有初始化),例如
const int RisingSunPuzzle::cols;
const int RisingSunPuzzle::rows;
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 如何在C++中使用非静态成员函数作为回调函数
- (C++)为什么静态成员可以在初始化之前使用
- 类的全局对象和静态成员
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- 模板化类中静态成员的延迟初始化
- 使用静态成员声明类时遇到问题
- C++:是否可以使用非静态成员变量模板?
- 静态成员函数使用相同的名称时出现模板类型名称错误
- 如何在复杂继承中访问静态成员变量
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 如何在友元函数中使用静态成员而不添加前缀 [类名]::
- C++构造函数和静态成员
- 为什么传递非静态成员函数会导致编译错误?
- 非静态成员失败的线程调用函数
- 静态成员变量不会由 gettext 转换
- decltype:使用指针访问类的静态成员
- 处理类内的回调时,必须调用对非静态成员函数的引用
- 当尝试使用它来初始化数组时,未识别C const静态成员
- singleton--cpp文件中无法识别的静态成员