0

I've been looking at this for hours and I can't figure it out. I'm just learning hierarchies now, and I can't understand why I'm getting an error for having an undefined base class.

Package.h

#include <string>
#include <iostream>
using namespace std;

class Package
{
//  Declare private member functions
protected:
    //  Sender's information variables
    string senderName;
    string senderAddress;
    string senderCity;
    int senderZip;

    //  Receiver's information variables
    string receiverName;
    string receiverAddress;
    string receiverCity;
    int receiverZip;

    //  Package information variables
    int weight;
    double costPerOunce;

//  Declare public member functions
public:
    //  Constrcutor declaration
    Package(const string &, const string &,
             const string &, const int &,
             const string &, const string &,
             const string &, const int &,
             int w, double cpo);

    //  Sender set function declarations
    void setSenderName(const string &);
    void setSenderAddress(const string &);
    void setSenderCity(const string &);
    void setSenderZip(const int &);

    //  Receiver set function declarations
    void setReceiverName(const string &);
    void setReceiverAddress(const string &);
    void setReceiverCity(const string &);
    void setReceiverZip(const int &);

    //  Sender get function declarations
    string getSenderName() const;
    string getSenderAddress() const;
    string getSenderCity() const;
    int getSenderZip() const;

    //  Receiver get function declarations
    string getReceiverName() const;
    string getReceiverAddress() const;
    string getReceiverCity() const;
    int getReceiverZip() const;

    //  get and set function declarations for package
    void setWeight(int);
    void setCostPerOunce(double);
    int getWeight() const;
    double getCostPerOunce() const;

    //  function declaration for package cost
    double calculateCost() const;

};  //end Package class definition

Package.cpp

 #include <iostream>
    #include <string>
    #include "Package.h"

Package::Package(const string &sName, const string &sAddress,
                 const string &sCity, const int &sZip,
                 const string &rName, const string &rAddress,
                 const string &rCity, const int &rZip,
                 int w, double cpo)
{
    senderName = sName;
    senderAddress = sAddress;
    senderCity = sCity;
    senderZip = sZip;
    receiverName = rName;
    receiverAddress = rAddress;
    receiverCity = rCity;
    receiverZip = rZip;
    weight = w;
    costPerOunce = cpo;
}

void Package::setSenderName(const string &sname)
{
    senderName = sname;
}

void Package::setSenderAddress(const string &sAddress)
{
    senderAddress = sAddress;
}

void Package::setSenderCity(const string &sCity)
{
    senderCity = sCity;
}

void Package::setSenderZip(const int &sZip)
{
    senderZip = sZip;
}

void Package::setReceiverName(const string &rName)
{
    receiverName = rName;
}

void Package::setReceiverAddress(const string &rAddress)
{
    receiverAddress = rAddress;
}

void Package::setReceiverCity(const string &rCity)
{
    receiverCity = rCity;
}

void Package::setReceiverZip(const int &rZip)
{
    receiverZip = rZip;
}

string Package::getSenderName() const
{
    return senderName;
}

string Package::getSenderAddress() const
{
    return senderAddress;
}

string Package::getSenderCity() const
{
    return senderCity;
}

int Package::getSenderZip() const
{
    return senderZip;
}

string Package::getReceiverName() const
{
    return receiverName;
}

string Package::getReceiverAddress() const
{
    return receiverAddress;
}

string Package::getReceiverCity() const
{
    return receiverCity;
}

int Package::getReceiverZip() const
{
    return receiverZip;
}

void Package::setWeight(int w)
{
    weight = w;
}

void Package::setCostPerOunce(double cpo)
{
    costPerOunce = cpo;
}

int Package::getWeight() const
{
    return weight;
}

double Package::getCostPerOunce() const
{
    return costPerOunce;
}

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

test.cpp

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

int main()
{
    //  Declare variables.
    Package parcel("name" , "address" , "city" , 00000 ,
        "name" , "address" , "city" , 00000 ,
        0.25 , 10);
    OvernightPackage nightPackage("name" , "address" , "city" , 00000 ,
        "name" , "address" , "city" , 00000 ,
        10 , 0.25 , 5.95);
    TwoDayPackage dayPackage("name" , "address" , "city" , 00000 ,
        "name" , "address" , "city" , 00000 ,
        10 , 0.25 , 0.15);

    //  Introduce the program.
    cout << "This program will output shipping information and costs" << endl;
    cout << "for different shipping methods." << endl << endl;

    //  Output sender's information
    cout << "Sender's Information:" << endl;
    cout << "Name: " << parcel.getSenderName() << endl;
    cout << "Address: " << parcel.getSenderAddress() << endl;
    cout << "City: " << parcel.getSenderCity() << endl;
    cout << "Zipcode: " << parcel.getSenderZip() << endl << endl;

    //  Output receiver's information
    cout << "Receiver's Information:" << endl;
    cout << "Name: " << parcel.getReceiverName() << endl;
    cout << "Address: " << parcel.getReceiverAddress() << endl;
    cout << "City: " << parcel.getReceiverCity() << endl;
    cout << "Zipcode: " << parcel.getReceiverZip() << endl << endl;

    //  Parcel information
    cout << "Package Information:" << endl;
    cout << "Weight (in ounces): " << parcel.getWeight() << endl << endl;

    //  Overnight Shipping information
    cout << "The price for overnight shipping for this parcel is: ";
    cout << nightPackage.calculateCost();

    //  Two-Day shipping information
    cout << "The price for two-day shipping for this parcel is: ";
    cout << dayPackage.calculateCost();

    return 0;   //return 0

}   //end main

Errors

 C2011: 'Package' : 'class' type redefinition
 see declaration of 'Package'
 error C2504: 'Package' : base class undefined
 error C2079: 'parcel' uses undefined class 'Package'
 error C2078: too many initializers
 error C2228: left of '.getSenderName' must have class/struct/union
 type is 'int'
 error C2228: left of '.getSenderAddress' must have class/struct/union
 type is 'int'
 error C2228: left of '.getSenderCity' must have class/struct/union
 type is 'int'
 error C2228: left of '.getSenderZip' must have class/struct/union
 type is 'int'
 error C2228: left of '.getReceiverName' must have class/struct/union
 type is 'int'
 error C2228: left of '.getReceiverAddress' must have class/struct/union
 type is 'int'
 error C2228: left of '.getReceiverCity' must have class/struct/union
 type is 'int'
 error C2228: left of '.getReceiverZip' must have class/struct/union
 type is 'int'
 error C2228: left of '.getWeight' must have class/struct/union
 type is 'int'

I'm new to this site, and did my best to provide information that I thought could be necessary. Let me know if I need anything else. Any advice as to what I'm doing wrong is appreciated. Thanks.

Pat Mulvihill
  • 53
  • 1
  • 12

1 Answers1

2

Package.h is being included multiple times. This causes the compiler to see the definition of the Package class multiple times, and this is the source of the first error message.

You need an include guard in this file to prevent multiple inclusions:

#ifndef PACKAGE_H
#define PACKAGE_H

// Contents of your Package.h file

#endif

As a side note, there is no reason to #include <iostream> in Package.h. Additionally, you should not put using namespace std; in header files, and I would strongly suggest that you avoid using it altogether.

Community
  • 1
  • 1
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • This worked. Thanks! Am I allowed to ask questions about other things on here that aren't related to errors but instead related to functionality (for instance, why is my weight set to 0 even though the constructor should set it to 10?), or do I need to create a new question? – Pat Mulvihill Dec 07 '14 at 11:34
  • @PatMulvihill Each issue should be a separate question. – cdhowie Dec 07 '14 at 12:35