首页 > 解决方案 > 在另一个成员函数中调用使用类属性的成员函数

问题描述

我有一个调用另一个成员函数的成员函数,在其中我使用类的私有属性,它需要另一个指向该类的指针作为参数。

现在,我想获得我的类的属性,并在计算后将它们复制到我作为参数的另一个对象指针。解释时有点混乱,但我相信这是一个简单的代码。然而,当我试图达到我班级的属性时,我得到了“分段错误”。代码如下:

在我的图像类中:

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);了,但也没有用。请帮忙。

标签: c++

解决方案


推荐阅读