我如何使用复制构造函数输出我的变量?



我试图创建一个复制构造函数来复制我的字符串,int和双变量后,从用户。我想要显示复制构造函数,并使用ship .output显示它。我想我已经创建了复制构造函数,但我仍然对如何调用复制构造函数以及如何在复制变量后实际显示它的内容感到困惑。查看我的教科书和各种youtube视频,我仍然对如何调用复制构造函数并显示它感到困惑。我认为我已经拥有的复制构造函数已经在复制变量,但我不确定。

在代码块上构建代码时,我也会遇到错误。错误:调用cargo::cargo()没有匹配的函数。我也不完全确定是什么原因造成的。

#include<iostream>
#include<string>
using namespace std;
class cargo{
public:
void setUnit(string unitName);
string getUnit() const;
void setAbbrev(string abbrevName);
string getAbbrev() const;
void setUnitID(string unitIDName);
string getUnitID() const;
void setAircraft(int aircraftname);
int getAircraft() const;
void setWeight(double weightName);
double getWeight() const;
void setDestinatoin(string destinationName);
string getDestination() const;
cargo(const cargo& other);
friend int weightConvert(int weightName);
void input();
void output();
void copy();
private:
string unit;
string abbrev;
string unitID;
int aircraft;
double weight;
string destination;
};
double weightConvert(double weightName){
weightName = weightName * 2.2046;
return weightName;
}
void cargo::setUnit(string unitName){
unit = unitName;
}
string cargo::getUnit() const{
return unit;
}
void cargo::setAbbrev(string abbrevName){
abbrev = abbrevName;
}
string cargo::getAbbrev() const{
return abbrev;
}
void cargo::setUnitID(string unitIDName){
unitID = unitIDName;
}
string cargo::getUnitID() const{
return unitID;
}
void cargo::setAircraft(int aircraftName){
aircraft = aircraftName;
}
int cargo::getAircraft() const{
return aircraft;
}
void cargo::setWeight(double weightName){
cout << "Is your weight in kilograms or pounds? k/p" << endl;
char x;
cin >> x;
if(x == 'k' || x == 'K'){
weight = weightConvert(weightName);
} else if(x == 'p' || x == 'P') {
weight = weightName;
}
}
double cargo::getWeight() const{
return weight;
}
void cargo::setDestinatoin(string destinationName){
destination = destinationName;
}
string cargo::getDestination() const{
return destination;
}
void cargo::input(){
string tempString;
int tempInt;
cout << "Is it a pallet or container" << endl;
getline(cin, tempString);
setUnit(tempString);
cout << "What is the load abbreviatoin?" << endl;
getline(cin, tempString);
setAbbrev(tempString);
cout << "What is the unitID?" << endl;
getline(cin, tempString);
setUnitID(tempString);
cout << "What is the aircraft type?" << endl;
cin >> tempInt;
setAircraft(tempInt);
cout << "Enter the weight of your unit." << endl;
double tempDouble;
cin >> tempDouble;
setWeight(tempDouble);
cout << "What is the destination for the unit?" << endl;
cin.ignore();
getline(cin, tempString);
setDestinatoin(tempString);
}
void cargo::output(){
cout << "Unit load type: " <<  getUnit() << endl;
cout << "Unit load abbreviation: " << getAbbrev() << endl;
cout << "Unit ID: " << getUnitID() << endl;
cout << "Aircraft type: " << getAircraft() << endl;
cout << "Weight: " << getWeight() << endl;
cout << "Destination: " << getDestination() << endl;
}
cargo::cargo(const cargo& other){
unit = other.getUnit();
abbrev = other.getAbbrev();
unitID = other.getUnitID();
weight = other.getWeight();
destination = other.getDestination();
}
int main(){
cargo shipment;
shipment.input();
shipment.output();
return 0;
}

我不太确定您想如何打印复制构造函数。也许在调用时打印一些东西?如果是这种情况,请在复制构造函数中添加print语句。这里是你的代码的问题:
复制构造函数是一个构造函数,当你写第一个构造函数时,c++编译器将停止为你写默认构造函数。因此,代码中唯一可用的构造函数是cargo(const cargo& other)。当你写cargo shipment时,它等于cargo shipment()。因此,您正在调用默认构造函数,该构造函数由于编译器的惰性(^_^)而不再存在。调用复制构造函数很简单,cargo shipment = cargo(a)假设a是您在其他地方构建的cargo实例。

最新更新