'类' type redefinition / base class undefined

时间:2014-12-07 11:19:36

标签: c++ 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

错误

 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'

我是这个网站的新手,并尽我所能提供我认为可能需要的信息。如果我还需要其他任何东西,请告诉我。对于我做错的任何建议都表示赞赏。感谢。

1 个答案:

答案 0 :(得分:2)

Package.h被多次收录。这会导致编译器多次查看Package类的定义,这是第一条错误消息的来源。

您需要在此文件中包含 include guard 以防止多次包含:

#ifndef PACKAGE_H
#define PACKAGE_H

// Contents of your Package.h file

#endif

作为旁注,#include <iostream>中没有Package.h的理由。此外,您不应将using namespace std;放入标题文件中,我强烈建议您avoid using it altogether