이 아래에있는 내 코드 :이 코드를 실행하면작문의 복사 생성자는 어떻게 작성합니까?
class Pulley{
private:
int noOfTeeth;
public:
Pulley();
Pulley(int teeth);
~Pulley();
void show();
};
Pulley::Pulley()
{
cout << "Pulley constructor called!";
noOfTeeth = 0;
}
Pulley::Pulley(int teeth)
{
cout << "Pulley constructor called!";
noOfTeeth = teeth;
}
void Pulley::show()
{
cout << "\n\nNo of Teeths of Pulley: " << noOfTeeth;
}
Pulley::~Pulley()
{
}
class GearBox{
private:
char *transmission;
Pulley p;
public:
GearBox();
GearBox(char *trans, int pTeeth);
~GearBox();
void show();
};
GearBox::GearBox(): p()
{
cout << "Gearbox constructor called!";
transmission = NULL;
}
GearBox::GearBox(char *trans, int pTeeth): p(pTeeth)
{
cout << "Gearbox constructor called!";
if(trans != NULL)
{
transmission = new char[strlen(trans)+1];
strcpy(transmission, trans);
}
else
{
transmission = NULL;
}
}
void GearBox::show()
{
cout << "\n\nTransmission of vehicle: " << transmission;
p.show();
}
GearBox::~GearBox()
{
}
class Vehicle{
private:
char *model;
char *color;
GearBox g;
public:
Vehicle();
Vehicle(char *mod, char *col, char *gr);
Vehicle(const Vehicle &vh);
~Vehicle();
void show();
};
Vehicle::Vehicle(): g()
{
cout << "Vehicle constructor called!";
model = NULL;
color = NULL;
}
Vehicle::Vehicle(char *mod, char *col, char *gr): g(gr)
{
cout << "Vehicle constructor called!";
if(mod != NULL)
{
model = new char[strlen(mod)+1];
strcpy(model, mod);
}
else
{
model = NULL;
}
if(col != NULL)
{
color = new char[strlen(col)+1];
strcpy(color, col);
}
else
{
color = NULL;
}
}
void Vehicle::show()
{
cout << "\n\nModel of Vehicle: " << model;
cout << "\n\nColor of Vehicle: " << color;
g.show();
}
int main()
{
Pulley p(20);
GearBox g("Manual", p);
Vehicle V("Honda", "Black", g);
V.show();
system("PAUSE");
}
지금, 나는 오류의 제비를 얻을, 나는 그 해결 방안을 무엇인지 모른다. 내가 이해 한 한 가지 오류는 Copy Constructor의 것이므로 아무도 내가 transmission, model and color
문자에 대한 포인터 용 복사 생성자를 쓸 수 있다는 것을 설명 할 수있다. 또한 다른 오류를 해결하는 방법을 알려주십시오. 고마워.
, 다음 번에 복잡성을 약간 추가합니다. 이 많은 코드에 많은 오류가있는 것은 그것이 발생했을 때 해결하지 않았기 때문입니다. ** 작동하지 않는 코드에는 절대로 추가하지 마십시오. ** 오류가 발생하는 동안 최대한이 코드를 단순화하는 것이 좋습니다. 버그가 분명 해지면 더 간단한 예제를 게시 할 수 있습니다. – Beta
문자열을 저장할 때 std :: string을 사용하십시오. 소멸자를 쓰거나 연산자를 복사하지 마십시오. 델리게이트 :: 문자열, std :: vector, std :: unique_ptr 등 –
_ 이제이 코드를 실행하면 많은 오류가 발생합니다. 그 코드와 그 해결 방법을 알지 못합니다 ._ From 이 문은 컴파일 오류가있는 경우 명확하지 않거나 (만약 그렇다면 오류가 무엇입니까?) 런타임에 코드가 실패했습니다 (그렇다면 디버거로 코드를 실행하여 실패한 이유를 찾으셨습니까?). –