类到字符串方法c ++

时间:2016-11-06 05:13:58

标签: c++

首先,抱歉格式错误,仍然试图习惯这个,但问题就在这里。我有一个类EndPoint,它是一堆字符串,还有一个名为Package的类,它是两个EndPoints和两个双精度数。使用这两个文件的.cpp文件在以下代码行中执行:

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"};  
Package regular{ homer, donald, 25.0, 0.20};

如何制作这些网格?我遇到的主要错误是EndPoint无法作为类读入到Package的字符串中。我真的很感激我能帮助你理解什么是错误的。

Package.cpp

#include "Package.h"
#include <iomanip>
#include <stdexcept>
#include <sstream>
#include <string>
using namespace std;

Package::Package(EndPoint Sender, EndPoint Reciever, double rate, double weight) {
    EndPoint SenderName = Sender;
    EndPoint RecieverName = Reciever;
    Rate = rate;
    Weight = weight;    
    setWeight(weight);
    setRate(rate);


}
 EndPoint:: EndPoint(const std::string&Endname, const std::string&EndAddress, const std::string&EndCity, const std::string&EndState, const std :: string&endzip)
{
    EndName = Endname;
    EndAddress1 = EndAddress;
    EndCity1 = EndCity;
    EndState1 = EndState;
    ZipCode1 = endzip;

};

void  Package::setSender(std::string Sender) {
    SenderName = Sender;
 }
string Package::getSender() { return SenderName; }
void  Package::setReciever(Reciever) {
    RecieverName = Reciever;
}
string Package::getRecieverName() const { return RecieverName; }


void  Package::setWeight(double weight) {
    if (weight < 0.0) {
        throw invalid_argument("Weight must be >= 0.0");
    }

    Weight = weight;
}
double Package::getWeight() const { return Weight; }

void  Package::setRate(double rate) {
    if (rate <= 0.0 || rate >= 1.0) {
        throw invalid_argument("Rate must be > 0.0 and < 1.0");
    }


    Rate = rate;
}
double Package::getRate() const { return Rate; }
double Package::calculateCost() const {
    return Weight * Rate;
}
string Package::toString() const {
    ostringstream regular;
    regular << fixed << setprecision(2); // two digits of precision   
    regular << "Sender Name: " << SenderName
        << "\n Reciever Name: " << RecieverName
        << "\n Rate: " << Rate
        << "\n Weight: " << Weight;
    return regular.str();
}


void  EndPoint::setEndName(const string& Endname) {
    EndName = Endname;
}
string EndPoint::getEndName() const { return EndName; }
void  EndPoint::setEndAddress(const string& EndAddress) {
    EndAddress1 = EndAddress;
}
string EndPoint::getEndAddress() const { return EndAddress1; }
string EndPoint::getCity() const { return EndCity1; }
void  EndPoint::setCity(const string& EndCity) {
    EndCity1 = EndCity;
}
string EndPoint::getState() const { return EndState1; }
void  EndPoint::setState(const string& EndState) {
    EndState1 = EndState;
}
string EndPoint::getZipCode() const { return ZipCode1; }
void  EndPoint::setZipCode(const string& ZipCode) {
    ZipCode1 = ZipCode;
}

Package.H

    #ifndef PACKAGE_H
#define PACKAGE_H
#include<string>


class Package {
public:
        Package(EndPoint, EndPoint, double = 0.0, double = 0.0);

        void setSender(EndPoint);
        std::string getSender() const;
        void setReciever(EndPoint);
        std::string getRecieverN() const;
        void setRate(double);
        double getRate() const;
        void setWeight(double);
        double getWeight() const;




    EndPoint SenderName;
    EndPoint RecieverName;
    double Rate;
    double Weight;
    double calculateCost() const; // calculate earnings
    std::string toString() const;
};
class EndPoint {
public:
    EndPoint(const std::string&, const std::string&, const std::string&, const std::string&, const std::string&);

    void setEndName(const std::string&);
    std::string getEndName() const;
    void setEndAddress(const std::string&);
    std::string getEndAddress() const;
    void setCity(const std::string&);
    std::string getCity() const;
    void setState(const std::string&);
    std::string getState() const;
    void setZipCode(const std::string&);
    std::string getZipCode() const;
protected:
    std::string EndName;
    std::string EndAddress1;
    std::string EndCity1;
    std::string EndState1;
    std::string ZipCode1;
    std::string toString() const;

};
#endif

1 个答案:

答案 0 :(得分:0)

由于Package需要了解EndPoint,因此您需要在EndPoint中的Package课程之前定义package.h课程。