0

First off, sorry for the bad formatting, still trying to get used to this, but here's the question. I have a class EndPoint that is a bunch of strings, and a class called Package that is two EndPoints and two doubles. The .cpp file that uses these two does it 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 the Swamp", "Leland", "MS", "38756"};  
Package regular{ homer, donald, 25.0, 0.20};

How do I make these mesh? The main error I am having is that EndPoint can't be read as a class into a string for Package. I really appreciate any help I can get to 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 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
user4581301
  • 33,082
  • 7
  • 33
  • 54
Sandwichy
  • 9
  • 3
  • If this is a compiler error, include the exact error being returned by your compiler in the question. – MrEricSir Nov 06 '16 at 05:16
  • seems you're missing the endpoint class include into package header – Elvis Dukaj Nov 06 '16 at 05:36
  • It's there, now just above the Package Class. – Sandwichy Nov 06 '16 at 05:44
  • Recommended reading: [Where should I prefer pass-by-reference or pass-by-value?](http://stackoverflow.com/questions/4986341/where-should-i-prefer-pass-by-reference-or-pass-by-value) – user4581301 Nov 06 '16 at 05:54
  • Also [read the section on overloading `operator<<` here.](http://stackoverflow.com/questions/4421706/operator-overloading) – user4581301 Nov 06 '16 at 05:58
  • That actually really helped. It compiled, but I don't think I'm out of the woods yet. – Sandwichy Nov 06 '16 at 06:02
  • 1
    @Sandwichy fixing the example in your question is not the right way to indicate that your problem is solved. You just need to accept the answer that solved the question, and vote it up if you think it's helpful. – HazemGomaa Nov 06 '16 at 06:12
  • 1
    Very true, @H.G . Hadn't noticed the question had been altered that much. Reverted the edit. Sandwichy, don't move the goalposts or radically alter a question after people have started to answer. It messes up the question -> answer flow and makes the answers look like they're answering a different question. Because they are. Once a question is solved and you have a new question, ask a new question. – user4581301 Nov 06 '16 at 06:20

1 Answers1

0

since Package needs to know about EndPoint, you need to have EndPoint class defined before Package class in package.h..

HazemGomaa
  • 1,620
  • 2
  • 14
  • 21
  • Okay, this has been done. The main error is with this line: Package regular{ homer, donald, 25.0, 0.20}; The first two "things" for lack of a better term right now, are supposed to be of Class EndPoint. Any edits to the constructor cause a slew of errors because those "things" are not strings anymore, but EndPoints. – Sandwichy Nov 06 '16 at 05:40
  • cannot convert from 'initializer list' to 'Package' on this line: Package regular{ homer, donald, 25.0, 0.20}; – Sandwichy Nov 06 '16 at 05:49
  • is `package.h` included before that line ?, do you have any other errors ? – HazemGomaa Nov 06 '16 at 05:56
  • HG watch your time here. OP's code is riddled with many, many syntax and logic errors. @Sandwichy write your programs in smaller chunks. If you write code one function a a time, compiling and testing before you move on, you won't suffer quite as much because if you only make small changes between test cycles you have a smaller area to inspect for bugs and syntax errors when things don't work Writing everything, then debugging put you in the position you're in now: Fix one bug, expose a dozen more. – user4581301 Nov 06 '16 at 06:02
  • Yeah it's a rabbit hole right now, or a knot. How exactly do I go about testing each part? I just don't see where I can properly cut it down into smaller pieces or what strategies I can use to test each part, cause one part of the program relies on another, all entwined. – Sandwichy Nov 06 '16 at 06:17
  • @Sandwichy you can start by having one class per header/cpp, for example. That will make things easier to debug and less error prone. – HazemGomaa Nov 06 '16 at 06:21
  • @Sandwichy Start with a skeleton class that defines nothing. Define one function, probably the constructor because it's hard to do much without a working constructor. Implement that one function. Test it to make sure it works. Define another function, implement it, rinse, repeat. When that class is done, move onto the next class. Sometimes to finish the first class you have to stop and make another class, but only work on one piece at a time. – user4581301 Nov 06 '16 at 06:23
  • And when you are testing and get stuck, the first thing to go to is your [development environment's debugger](https://en.wikipedia.org/wiki/Debugger). The debugger is the bomb. It allows you to control the execution of the program, running it instruction by instruction if you have to, and inspect the variables as you go. It makes tracing the flow of the program easy and you can see what variable values made the program go off the rails. Push a button, see what happened, push the button again, and again and again, until you see the problem and bury your head in your hands and groan in disgust – user4581301 Nov 06 '16 at 06:30