简体   繁体   English

从另一个类的成员函数编辑私有类成员

[英]Editing private class member from a member function for another class

My two classes are:我的两个班级是:

class sequence
{
public:
...

private:
int number_of_samples;
float* samples;
};

class FIR
{
public:
...

sequence* apply_filter(const sequence& f);

private:
int number_of_coefficients;
float* coefficients;
};

When I run the following member function:当我运行以下成员函数时:

sequence* FIR::apply_filter(const sequence& f)
{
ofstream output_sequence_file("output.txt");
output_sequence_file<<number_of_coefficients<<endl;
sequence* Vout = new sequence;
for(int i=0;i<number_of_coefficients;i++)
{
    if (0==i) Vout->samples[i]=coefficients[i]*f.samples[i];
    else Vout->samples[i]=coefficients[i] * f.samples[i] + Vout->samples[i-1];

output_sequence_file<<Vout->samples[i]<<endl;
}
}

I get an error saying: 'samples' is a private member of sequence.我收到一条错误消息:'samples' 是序列的私有成员。

Any help?有什么帮助吗? Thank you谢谢

Move samples into the public section of class sequence or declare FIR::apply_filter() a friend of class sequence .samples移动到class sequence的公共部分或将FIR::apply_filter()声明为class sequencefriend Private members of a class cannot be accessed by other classes or functions unless they have been granted the privilege.一个类的私有成员不能被其他类或函数访问,除非他们被授予特权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM