Class to string method c++ -
first off, sorry bad formatting, still trying used this, here's question. have class endpoint bunch of strings, , class called package 2 endpoints , 2 doubles. .cpp file uses these 2 in these lines of code:
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 swamp", "leland", "ms", "38756"}; package regular{ homer, donald, 25.0, 0.20};
how make these mesh? main error having endpoint can't read class string package. appreciate can understand what's wrong.
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 >= 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 > 0.0 , < 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); // 2 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
since package
needs know endpoint
, need have endpoint
class defined before package
class in package.h
..
Comments
Post a Comment