왜 Rectangle 객체를 만들 때 분할 오류가 발생합니까? 내 생성자에 대해 잘못된 것이 있다고 생각하지만 C++에서 무엇을 식별하는지에 대한 충분한 경험이 없습니다.기본 클래스 생성자의 분할 오류
#include <string>
#include <map>
using namespace std;
class Shape {
private:
string name;
string property_name;
map<string,double> parameters;
public:
Shape(){
}
void set_name(string n){
name=n;
}
string set_property_name(string s){
property_name=s;
}
void set_parameter(string p, double n){
parameters[p]=n;
}
double get_parameter(string p) {
return parameters[p];
}
virtual double get_property() = 0;
};
class Shape2D: public Shape {
public:
Shape2D() {
set_property_name("area");
}
};
class Rectangle: public Shape2D {
public:
Rectangle() {
set_name("rectangle");
set_parameter("length",0);
set_parameter("base",0);
}
double get_property() {
return get_parameter("length") * get_parameter("base");
}
};
int main() {
Shape * user_shape;
user_shape=new Rectangle();
return 0;
}
여기에 잘 작동 정의되지 않은 동작이기 때문입니다. 재현 할 수 없습니다. 코드는 완벽하게 잘 보인다. 디버거를 사용하여 무엇이 잘못되었는지 찾으십시오. –
'set_property_name (string s)'는'string' 대신'void' 유형이어야합니다. 문제를 재현 할 수 없습니다. –
@ Jean-FrançoisFabre 더 나은 컴파일러 나 더 높은 경고가 필요합니다. http://coliru.stacked-crooked.com/a/7bee0da403209195 – NathanOliver