错误C2248无法访问在类中声明的私有成员 [英] error C2248 cannot access private member declared in class

查看:421
本文介绍了错误C2248无法访问在类中声明的私有成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在c ++中练习。老师给了我们在类分配的公共部分的功能(所以我不能改变header.h中函数的公开声明)。我有一个编译错误,当我试图做一个朋友cout函数:编译器说错误4错误C2248:'biumath :: Assignment :: m_rowsOfVarArray':无法访问在'biumath :: Assignment'中声明的私有成员。我认为问题在于命名空间。

We got an exercise in c++. The teacher gave us the functions in the public part of the "class Assignment"(so I cannot change the public declaration of the functions in the header.h). I got an compilation error when i tried to make a friend cout function: the compiler say "Error 4 error C2248: 'biumath::Assignment::m_rowsOfVarArray' : cannot access private member declared in class 'biumath::Assignment'". I thinks that the problem is with the namespaces.

biumath.h

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

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;
}

我不能更改类的公共部分。感谢!

again I cannot change the public part of the class. thanks!

推荐答案

您必须编写或使用 public 顺序更改您的变量。
您可以使用 private 避免对变量进行任何意外或未经授权的更改。所以,只有你的 public 方法能够正确地更改。

You have to write or use a public method in order to change your variable. You use private to avoid any unexpected or unauthorised change of your variables. So, that only your public method is able to change it properly.

这篇关于错误C2248无法访问在类中声明的私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆