c++ - 在另一个成员函数中调用使用类属性的成员函数
问题描述
我有一个调用另一个成员函数的成员函数,在其中我使用类的私有属性,它需要另一个指向该类的指针作为参数。
现在,我想获得我的类的属性,并在计算后将它们复制到我作为参数的另一个对象指针。解释时有点混乱,但我相信这是一个简单的代码。然而,当我试图达到我班级的属性时,我得到了“分段错误”。代码如下:
在我的图像类中:
Image *Image::new_gray(int width, int height)
{
return new Image(width, height, 1);
}
void Image::box_filter_x(Image *buffer, int n)
{
printf("in box x func.\n");
printf("%d\n", m_width);
//m_width is class attribute. here i get segmentation fault and
//rest of the code doesn't run, obviously.
printf("m_width read, calculating further.\n");
....
buffer->m_width = m_width;
....
}
void Image::box_filter(Image *dst, int n)
{
printf("%d\n", m_width);
//this works as expected.
box_filter_x(dst, n);
//this is where i call my function.
}
在我进行测试的另一个文件中的主函数内部:
int main(int argc, char** argv)
{
Image* gray = Image::new_gray(128, 128);
...
Image* gray1 = Image::new_gray(128, 128);
...
gray->box_filter(gray1, 3);
}
我试过this->box_filter_x(dst,n);
了,但也没有用。请帮忙。
解决方案
推荐阅读
- r - 无法安装 ebaytd 软件包
- web-ide - WebIDE 无法连接到 adb 连接设备上的远程运行时
- javascript - 如何在不改变原始对象的情况下创建对象的深度冻结版本?
- java - jdbcTemplate#query 只返回第一行而不是全部
- python - 如何从通过python从mysql提取的数据中绘制图表?
- python-3.x - 如何在 Django `DeleteView` 中为`success_url` 传递参数(slug)
- java - ActiveMQ 和 Java - 发布和消费之间的延迟
- flutter - Flutter 分页数据表
- laravel - Laravel:渴望加载特定的列
- java - 为什么我收到变量未初始化错误?