有关多个类和标题的送货程序中的继承,隐私和对象的C ++问题

时间:2016-11-04 19:43:44

标签: c++ pointers inheritance data-members

我的程序应该打印一个源自EndPoint toString方法的“to”和“from”地址,但我无法弄清楚如何实现它。这是我的代码。如何在Package :: Package构造函数中获取toString方法以打印EndPoint的toString方法的内容?

// ShippingProgram.cpp :
//

#include "stdafx.h"
#include <iomanip>
#include <stdexcept>
#include <sstream>
#include <iostream>
#include "Package.h" // Package class definition
using namespace std;

// constructor 

EndPoint::EndPoint(const string& nameInfo, const string& addressInfo, const string& cityInfo, const string& stateInfo, int zipInfo) {
    name = nameInfo;
    address = addressInfo;
    city = cityInfo;
    state = stateInfo;
    zip = zipInfo;
}


string EndPoint::toString() const {
    ostringstream output;
    output << fixed << setprecision(2); // two digits of precision   
    output << name << "\n" << address << "\n" <<
        city << ", " << state << " " << zip;
    return output.str();
}


Package::Package(EndPoint senderInfo, EndPoint receiverInfo, double weightInfo, double costPerOzInfo) {

    weight = weightInfo; // should validate                     
    costPerOz = costPerOzInfo;
}


void Package::calculateCost(double)
{
}

double Package::calculateCost() const {
    return weight * costPerOz;
}


string Package::toString() const {
    ostringstream output;
    output << fixed << setprecision(2); // two digits of precision   
    output << "\nFrom:\n" << senderInfo.toString() << "\n\nTo:\n" <<    receiver << 
        "\n\nWeight: " << weight << endl <<
        "\nShipping cost:\n" << calculateCost();
    return output.str();
}

主要方法:

#include <iostream>
#include <iomanip>
#include "stdafx.h"
//#include "TwoDayPackage.h"
//#include "OvernightPackage.h"
#include "Package.h"
using namespace std;

int main()
{
    // Three address records.

    EndPoint homer{ "Homer Simpson", "742 Evergreen Terrace", "Springfield",
        "FL", 32401 };

    EndPoint donald{ "Donald Duck", "1313 Webfoot Walk", "Duckburg",
        "CA", 95501};

    EndPoint kermit{ "Kermit Frog", "On the Swamp", "Leland", "MS", 38756 };

    // This calls the base class constructor (regular fee).

    Package regular{ homer, donald, 25.0, 0.20 };

    // Defines output precision for floating point numbers (iomanip).

//  cout << fixed << setprecision(2);

    // Prints package parameters.

    cout << "Regular package processed." << endl;
    cout << regular.toString();
    cout << "Shipping Cost: $" << regular.calculateCost() << endl << endl;
    cout << homer.toString(); 
    // First derived class (two-day fee added).

    /* TwoDayPackage twoday{ donald, kermit, 17.5, 0.20, 2.0 };

    cout << "Two-day package processed." << endl;
    cout << twoday.toString();
    cout << "Shipping Cost: $" << twoday.calculateCost() << endl << endl;

    // Second derived class (overnight fee added).

    OvernightPackage overnight{ kermit, homer, 14.2, 0.20, 0.50 };

    cout << "Overnight package processed." << endl;
    cout << overnight.toString();
    cout << "Shipping Cost: $" << overnight.calculateCost() << endl << endl;
    */
    }

这个项目要求我创建一个带继承的运输程序。它必须包含一个私有的“EndPoint”类,它包含发送者和接收者信息以及一个“Package”类,它编译所有内容并将其放入字符串。

我的错误是在世界上如何让我的Package构造函数能够包含来自我的EndPoint类的信息。由于main方法的格式是Package类必须(EndPoint,EndPoint,Weight,Cost),但它不会像那样编译。我想我只是不明白如何将EndPoint信息发送到Package对象。

以下是我的错误:

  1. 没有构造函数"Package::Package" matches the argument list argument types are: (EndPoint, EndPoint, double, double)
  2. 的实例
  3. Error C2440 'initializing': cannot convert from 'initializer list' to 'Package'
  4. Error C3861 'setprecision': identifier not found
  5. Package.h

    #pragma once
    #ifndef PACKAGE_H
    #define PACKAGE_H    
    #include <string>   
    #include <iostream>
    using namespace std;
    
    class EndPoint {
    public:
        EndPoint(const std::string&, const std::string&, const std::string&, const std::string&, int = 0.0);
    
        void setName(const std::string&);
        std::string getName() const;
    
        void setAddress(const std::string&);
        std::string getAddresss() const;
    
        void setCity(const std::string&);
        std::string getCity() const;
    
        void setState(const std::string&);
        std::string getState() const;
    
        void setZip(int);
        int getZip() const;
    
        string toString() const;
    protected:
        std::string name;
        std::string address;
        std::string city;
        std::string state;
    
        int zip;
    };
    class Package {
    public:
        string toString() const;
        Package(const std::string&, const std::string&, double = 0.0, double = 0.0);
    
        void setSender(const std::string&);
        std::string getSender() const;
    
        void setReceiver(const std::string&);
        std::string getReceiver() const;
    
        void setWeight(double);
        double getWeight() const;
    
        void setCostPerOz(double);
        double getCostPerOz() const;
    
        void calculateCost(double);
        double calculateCost() const;
    
        double calculateCost(double weight, double costPerOz)
        {
            double shipping;
            shipping = weight * costPerOz;
    
            cout << "The Base Cost = " << shipping << endl << endl;
            return shipping;
        }
    
    
    
    protected:
        std::string sender;
        std::string receiver;
        double weight; // gross weekly sales
        double costPerOz; // commission percentage
    };
    
    #endif
    

    Package.cpp

    #include "stdafx.h"
    #include <iomanip>
    #include <stdexcept>
    #include <sstream>
    #include "Package.h" // Package class definition
    using namespace std;
    
    // constructor 
    
    EndPoint::EndPoint(const string& nameInfo, const string& addressInfo, const string& cityInfo, const string& stateInfo, int zipInfo) {
        name = nameInfo;
        address = addressInfo;
        city = cityInfo;
        state = stateInfo;
        zip = zipInfo;
    }
    
    void EndPoint::setName(const string& nameInfo) {
        name = nameInfo;
    }
    
    string EndPoint::getName() const { return name; }
    
    void EndPoint::setAddress(const string& addressInfo) {
        address = addressInfo;
    }
    
    string EndPoint::getAddresss() const { return address; }
    
    void EndPoint::setCity(const string& cityInfo) {
        city = cityInfo;
    }
    
    string EndPoint::getCity() const { return city; }
    
    void EndPoint::setState(const string& stateInfo) {
        state = stateInfo;
    }
    
    string EndPoint::getState() const { return state; }
    
    void EndPoint::setZip(int zipInfo) {
        zip = zipInfo;
    }
    
    int EndPoint::getZip() const {
        return zip;
    }
    
    string EndPoint::toString() const {
        ostringstream output;
        output << fixed << setprecision(2); // two digits of precision   
        output << name << "\n" << address << "\n" <<
            city << ", " << state << " " << zip;
        return output.str();
    }
    string EndPoint::getState() const { return state; }
    Package::Package(const string& senderInfo, const string& receiverInfo, double weightInfo, double costPerOzInfo) {
        sender = senderInfo; // should validate                              
        receiver = receiverInfo; // should validate                                
        weight = weightInfo; // should validate                     
        costPerOz = costPerOzInfo;
    }
    
    
    void Package::setSender(const string& senderInfo) {
        sender = senderInfo; // should validate
    }
    
    
    string Package::getSender() const { return sender; }
    
    
    void Package::setReceiver(const string& receiverInfo) {
        receiver = receiverInfo; // should validate
    }
    
    
    string Package::getReceiver() const { return receiver; }
    
    
    
    void Package::setWeight(double weightInfo) {
        if (weightInfo < 0.0) {
            throw invalid_argument("The package weight must be >= 0.0");
        }
    
        weight = weightInfo;
    }
    
    
    double Package::getWeight() const { return weight; }
    
    
    void Package::setCostPerOz(double costPerOzInfo) {
    
        costPerOz = costPerOzInfo;
    }
    
    
    double Package::getCostPerOz() const {
        return costPerOz;
    }
    
    
    double Package::calculateCost() const {
        return weight * costPerOz;
    }
    
    
    string Package::toString() const {
        ostringstream output;
        output << fixed << setprecision(2); // two digits of precision   
        output << "From:\n" << sender << "\n\nTo:\n" << receiver << 
            "\n\nWeight: " << weight << endl <<
            "\nShipping cost: " << calculateCost();
        return output.str();
    }
    

    Main.cpp的

    #include <iostream>
    #include <iomanip>
    #include "stdafx.h"
    //#include "TwoDayPackage.h"
    //#include "OvernightPackage.h"
    #include "Package.h"
    using namespace std;
    
    int main()
    {
        // Three address records.
    
        EndPoint homer{ "Homer Simpson", "742 Evergreen Terrace", "Springfield",
            "FL", 32401 };
    
        EndPoint donald{ "Donald Duck", "1313 Webfoot Walk", "Duckburg",
            "CA", 95501};
    
        EndPoint kermit{ "Kermit Frog", "On the Swamp", "Leland", "MS", 38756 };
    
        // This calls the base class constructor (regular fee).
    
        Package regular{ homer, donald, 25.0, 0.20 };
    
        // Defines output precision for floating point numbers (iomanip).
    
        cout << fixed << setprecision(2);
    
        // Prints package parameters.
    
        cout << "Regular package processed." << endl;
        cout << regular.toString();
        cout << "Shipping Cost: $" << regular.calculateCost() << endl << endl;
    
        // First derived class (two-day fee added).
    
        /* TwoDayPackage twoday{ donald, kermit, 17.5, 0.20, 2.0 };
    
        cout << "Two-day package processed." << endl;
        cout << twoday.toString();
        cout << "Shipping Cost: $" << twoday.calculateCost() << endl << endl;
    
        // Second derived class (overnight fee added).
    
        OvernightPackage overnight{ kermit, homer, 14.2, 0.20, 0.50 };
    
        cout << "Overnight package processed." << endl;
        cout << overnight.toString();
        cout << "Shipping Cost: $" << overnight.calculateCost() << endl << endl;
        */
        }
    

    我已经在这里注释了代码块,因为我试图让第一部分工作,然后再深入研究。

    编辑:

    谢谢大家的建议!我已经做了一些编辑并取出了大量额外的代码(getter和setter。我用java学习了......)并且我已经得到了程序来编译和按预期工作,除了一个小但重要的问题。

1 个答案:

答案 0 :(得分:1)

  

没有构造函数的实例&#34; Package :: Package&#34;匹配参数   list参数类型是:(EndPoint,EndPoint,double,double)

代码中的


Package regular{ homer, donald, 25.0, 0.20 };您正在将错误的变量传递给构造函数的参数,这些参数是第一个和第二个参数的Endpoint对象

你拥有的是:

Package(const std::string&, const std::string&, double = 0.0, double = 0.0);

接受第一个和第二个参数的std :: string对象。

  

无法转换为&#39;初始化列表&#39;到&#39;包

修复问题1将解决此问题

我不知道为什么你得到第3名,因为你的主要是