代码块和 G++ 错误"is private"
code blocks and g++ error "is private"
我在代码块中遇到这些错误 IDE(使用 g++ 编译器)每个成员变量的"是私有的"。据我所知,在其他成员中使用私有变量是合法的,这就是我正在做的事情。这是我的 cpp 的代码:
/*
bullsAndCows.cpp
*/
using namespace std;
//enum class state {_bull, _cow, _none};
class bullsAndCows {
private:
const int m_size{4};
bool m_guessed{false};
std::vector<char> m_digit;
std::vector<state> m_digitState;
public:
bullsAndCows() {
m_guessed = false;
for(int i = 0; i < m_size; i++)
m_digitState[i] = state._none;
}
void bullsAndCows::setGuessed(bool value) { _guessed = value; }
bool bullsAndCows::getGuessed() { return _guessed; }
void bullsAndCows::setDigit(char value, int i) { m_digit[i] = value; }
char bullsAndCows::getDigit(int i) { return m_digit[i]; }
void bullsAndCows::setDigitState(state value, int i) { m_digitState[i] = value; }
state bullsAndCows::getDigitState(int i) { return m_digitState[i]; }
};
这是我的主要代码,我正在测试:
#include "bullsAndCows.h"
using namespace std;
int main()
{
bullsAndCows game;
for(int i = 0; i < game.m_size; i++) {
cin >> game.m_digit[i];
cout << game.m_digit[i];
}
return 0;
}
c++11 标志在编译器上激活。
编译器是正确的。
声明:
cin >> game.m_digit[i];
正在访问私有成员:
class bullsAndCows {
private:
const int m_size{4};
bool m_guessed{false};
std::vector<char> m_digit;
std::vector<state> m_digitState;
私有成员只能通过类内的方法访问,而不能由外部实体(如 main
函数)访问。
您的选择是:
- 创建 getter 和 setter 方法。
- 过载流提取运算符>>用于类
bullsAndCows
- 公开变量。
相关文章:
- Why is UINT32_MAX + 1 = 0?
- C++错误:"error: int aaa::bbb is protected within this context"
- 创建具有 new in 函数和"this is nullptr"异常的对象
- std::make_shared和protected/private构造函数
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- Is !NaN not a NaN?
- Directx 11 - CompileFromFile() is not compiling
- Centos7 g++ "to_string is not in a member of std"
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- Where is std::hardware_destructive_interference_size?
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- What is unordered_set in C++
- QString& QString::operator=(const QByteArray&)' is private
- "class::data member is private"错误,但我正在使用成员函数对其进行操作?
- "is private within this context"被抛出用于不应是私有的函数(GCC 5.3.0,C++11)
- 代码块和 G++ 错误"is private"
- 为什么在使用类成员访问其他类的私有成员时出现"is private"错误?
- 尝试编译 boost 依赖库 - "is private"错误
- C++。无法调用成员函数。错误 : "foo is private"
- CvSVM(const CvSVM&)' is private