无法访问类中声明的私有成员
error C2248 cannot access private member declared in class
我们用c++做了一个练习。老师在"class Assignment"的public部分给了我们函数(所以我不能改变header.h中函数的public声明)。当我试图使朋友计数函数时,我得到了一个编译错误:编译器说"错误4错误C2248: 'biumath::Assignment::m_rowsOfVarArray':无法访问在类'biumath::Assignment'中声明的私有成员"。我认为问题出在命名空间上。
biumath.h
#ifndef BIUMATH_H
#define BIUMATH_H
#include <iostream>
#include <string>
//using namespace std;
namespace biumath
{
class Assignment
{
private:
int **m_varArray;
int m_rowsOfVarArray;
public:
Assignment(); //1
Assignment(char symbol, double value); //2
bool containsValueFor(char symbol) const; //3
double valueOf(char symbol) const; //4
void add(char symbol, double val); //5
friend std::ostream& operator<< (std::ostream& out,
const Assignment& assignment); //6
};
}
#endif
biumath.cpp
#include <iostream>
#include "biumath.h"
using namespace biumath;
using namespace std;
std::ostream& operator<< (std::ostream& out,
const Assignment& assignment)
{
out<<assignment.m_rowsOfVarArray<<std::endl;
//return the stream. cout print the stream result.
return out;
}
还是不能更改类的公共部分。谢谢!
您的错误信息解释了这个问题。属性m_rowsOfVarArray
被声明为private
,这意味着您不能在类之外对它进行读取或写入。要解决这个问题,您需要将其更改为public
或编写一个访问器函数来检索值。
您必须编写或使用public
方法才能更改变量。使用private
可以避免对变量进行任何意外或未经授权的更改。所以,只有你的public
方法能够正确地改变它。
相关文章:
- 使用静态成员声明类时遇到问题
- 错误 C2059:语法错误:从结构成员声明'constant'
- std::d eclare_if 或其他在编译时丢弃成员声明的假设方法
- C++ 成员声明中不允许使用限定名称
- 又一个"未找到成员声明"
- auto f = bind(dist<>,gen) 在成员声明C++失败。未被识别为函数
- C++错误:成员声明末尾的预期“;”
- 静态成员声明 c++11
- 如何将成员声明为指向外部"C"函数的指针?
- C++11 - 将非静态数据成员声明为 'auto'
- 类成员声明的快捷方式
- libclang 获取成员声明
- 将成员声明与enable_if一起使用
- 找不到成员声明
- C++错误:找不到成员声明
- 找不到c++成员声明
- 成员声明顺序在类中如果彼此依赖,则为最优解
- 将struct的成员声明为uint32_t时额外的字节
- 为什么类的大小取决于成员声明的顺序?以及如何
- 是成员声明`decltype(name)name;`在第一个名称引用封闭作用域的本地结构中允许