-3

My program is supposed to print a "to" and "from" address sourced from the EndPoint toString method but I can't quite figure out how to implement it. Here is my code. How do I get the toString method in the Package::Package constructor to print the contents of the EndPoint's toString method?

// 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();
}

Main Method:

#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;
    */
    }

This project requires that I create a shipping program with inheritance. It must include a "EndPoint" class that is private and contains the sender and receiver info and a "Package" class that compiles everything and puts it to string.

My Errors are with how in the world I get my Package constructor to be able to contain the information from my EndPoint class. Since the main method is formatted where the Package class must be (EndPoint, EndPoint, Weight, Cost) but it doesn't compile like that. I guess I just don't understand how to send the EndPoint info to the Package objects.

Here are my errors:

  1. No instance of constructor "Package::Package" matches the argument list argument types are: (EndPoint, EndPoint, double, double)
  2. Error C2440 'initializing': cannot convert from 'initializer list' to 'Package'
  3. Error C3861 'setprecision': identifier not found

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;
    */
    }

I have commented out blocks of code here as I am trying to just get the first part to work before diving into the rest.

Edit:

Thank you all for the advice! I have made some edits and taken out a ton of extra code (getters and setter. I learned with java...) and I have gotten the program to compile and work as intended save for a small but important issue.

halfer
  • 19,824
  • 17
  • 99
  • 186
Bren Bailey
  • 11
  • 1
  • 1
  • 8
  • 1
    Please [edit] your question to provide a [mcve]. – Baum mit Augen Nov 04 '16 at 19:46
  • BTW, you don't need to use *precompiled headers* or `stdafx.h`. For smaller projects, there is not a reduction in build time, especially in development. – Thomas Matthews Nov 04 '16 at 19:57
  • I recommend placing `EndPoint` into separate header and source files. – Thomas Matthews Nov 04 '16 at 19:58
  • The easiest fix: `#include ` should take care of `setprecision`. – Thomas Matthews Nov 04 '16 at 20:00
  • I've edited my code and implemented some of y'alls advice but come across an interesting problem that I've edited my original question to include. Thanks! – Bren Bailey Nov 04 '16 at 23:21
  • Don't worry about downvotes, they are just messages to communicate something (see the alt-text on the arrow buttons to see generally what they mean). We generally discourage meta commentary in posts here, so if you want to comment on downvotes, please (a) do it in the comments, and (b) keep it constructive and diplomatic. – halfer Nov 05 '16 at 18:21

1 Answers1

1

No instance of constructor "Package::Package" matches the argument list argument types are: (EndPoint, EndPoint, double, double)

in your code:
Package regular{ homer, donald, 25.0, 0.20 };
you are passing wrong variables to the constructor's parameters which is an Endpoint object for first and second parameter

what you have is:

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

which accepts a std::string object for first and second parameter.

cannot convert from 'initializer list' to 'Package

fixing problem 1 will fix this

i dont know why you get 3rd one since you have iomanip in your main

Lorence Hernandez
  • 1,189
  • 12
  • 23
  • In the instructions for this project it says to set endpoint to private (which I didn't do because it breaks everything). I just don't get it. If I change everything to work with endpoints I get way more errors with mismatched types and it says there isn't a default endpoint constructor but if I create one boom another error.... I am pulling my hair out here. how do I pass an endpoint to the package constructor? – Bren Bailey Nov 04 '16 at 20:55
  • @BrenBailey sorry but i fell asleep yesterday, to pass endpoint to the constructor you have to make it accept an endpoint to the parameter just like u did in the string or double like this Package(const EndPoint& ep,const EndPoint& ,double,double) , you can overload it by the way. – Lorence Hernandez Nov 05 '16 at 11:07