-1

I'm trying to create a copy constructor to copy my string, int and double variables after taking it in from the user. I want to display the copy constructor as well as display it using the shipment.output. I think I have created the copy constructor but I'm still confused on how to call the copy constructor and how to actually display it's content after copying the variables. Looking on my textbook and various youtube videos I'm still confused on how to call a copy constructor and display it. I think the copy constructor I already have is already copying the variables but I am not sure.

I am also getting and error when building the code on code blocks. "Error: No matching function for call to 'cargo::cargo()' ". Not entirely sure what's causing that either.

#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;
}
  • for solve error, create default constructor like `cargo() { /* init default values */ };`. You can use copy structor like `cargo cargo_copy = shipment;` – Zem Feb 25 '21 at 02:39
  • 1
    I am not seeing anything in your class that should need you to define a custom copy constructor. Also, copy constructors do not have anything to do with output. – Zan Lynx Feb 25 '21 at 03:02
  • Check out https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – Zan Lynx Feb 25 '21 at 03:06

1 Answers1

-1

I am not quite sure how you want to print the copy constructor. Maybe print something when it gets called? If that is the case, add a print statement in the copy constructor. And here is the problem with your code:
Copy constructor is a constructor, and C++ compiler will stop writing you a default constructor when you write your very first constructor. So the only constructor available in your code is cargo(const cargo& other). When you write cargo shipment, it is equivalent to cargo shipment(). So you are calling the default constructor which no longer exists by the laziness of the compiler(^_^). To call the copy constructor is simple, cargo shipment = cargo(a) assuming a is a cargo instance you build somewhere else.