从 matlab 调用 class 时出现分段错误
Segmentation fault when calling class from matlab
我遇到以下代码的分段错误,我真的不知道这里发生了什么。好像跟Master-class里面的指针有关系,但是不知道怎么解决。我有以下代码:
class Shape
{
public:
Shape(){}
~Shape(){}
virtual void draw() = 0;
};
class Circle : public Shape
{
public:
Circle(){}
~Circle(){}
void draw()
{
printf("circle");
// code for drawing circle
}
};
class Line : public Shape
{
public:
Line() {}
~Line() {}
void draw()
{
printf("line");
// code for drawing line
}
};
class Master
{
public:
Shape* member_shape;
public:
Master()
{}
~Master()
{}
void add_shape_circle()
{
member_shape = new Circle();
}
void add_shape_line()
{
member_shape = new Line();
}
};
Master* master_object;
你知道如何让这段代码工作吗?谢谢。
编辑(添加主要功能):
实际上我的代码中不存在像下面这样的主函数,因为我在 MATLAB c-mex 函数中使用代码。但它应该是这样的:
//... classes from above here
int main()
{
master_object = new Master();
master_object->add_shape_circle();
master_object->member_shape->draw(); // segmentation error here
return 0;
}
- 编辑
如果我直接在 Master
构造函数中实例化 Circle
对象,则不会发生错误。但是那就没办法在Line
和Circle
之间选择了。示例:如果我将 Master
class 更改为以下内容,则函数调用 master_object->member_shape->draw()
不会导致错误。
class Master
{
public:
Shape* member_shape;
public:
Master()
{
member_shape = new Circle();
}
~Master()
{}
void add_shape_circle(){}
void add_shape_line(){}
};
所以,这个未初始化的指针有问题......我想。
问题可能与您使用 printf
有关。来自 https://www.mathworks.com/help/matlab/matlab_external/creating-c-mex-files.html?requestedDomain=www.mathworks.com :
Using cout or the C-language printf function does not work as expected in C++ MEX files. Use the mexPrintf function instead.
我遇到以下代码的分段错误,我真的不知道这里发生了什么。好像跟Master-class里面的指针有关系,但是不知道怎么解决。我有以下代码:
class Shape
{
public:
Shape(){}
~Shape(){}
virtual void draw() = 0;
};
class Circle : public Shape
{
public:
Circle(){}
~Circle(){}
void draw()
{
printf("circle");
// code for drawing circle
}
};
class Line : public Shape
{
public:
Line() {}
~Line() {}
void draw()
{
printf("line");
// code for drawing line
}
};
class Master
{
public:
Shape* member_shape;
public:
Master()
{}
~Master()
{}
void add_shape_circle()
{
member_shape = new Circle();
}
void add_shape_line()
{
member_shape = new Line();
}
};
Master* master_object;
你知道如何让这段代码工作吗?谢谢。
编辑(添加主要功能):
实际上我的代码中不存在像下面这样的主函数,因为我在 MATLAB c-mex 函数中使用代码。但它应该是这样的:
//... classes from above here
int main()
{
master_object = new Master();
master_object->add_shape_circle();
master_object->member_shape->draw(); // segmentation error here
return 0;
}
- 编辑
如果我直接在 Master
构造函数中实例化 Circle
对象,则不会发生错误。但是那就没办法在Line
和Circle
之间选择了。示例:如果我将 Master
class 更改为以下内容,则函数调用 master_object->member_shape->draw()
不会导致错误。
class Master
{
public:
Shape* member_shape;
public:
Master()
{
member_shape = new Circle();
}
~Master()
{}
void add_shape_circle(){}
void add_shape_line(){}
};
所以,这个未初始化的指针有问题......我想。
问题可能与您使用 printf
有关。来自 https://www.mathworks.com/help/matlab/matlab_external/creating-c-mex-files.html?requestedDomain=www.mathworks.com :
Using cout or the C-language printf function does not work as expected in C++ MEX files. Use the mexPrintf function instead.