'class'类型重定义/基类未定义



我已经看了几个小时了,我无法弄清楚。 我现在只是在学习层次结构,我不明白为什么我会收到一个未定义的基类的错误。

包.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

包装.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;
}

测试.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'

我是这个网站的新手,并尽力提供我认为必要的信息。 如果我需要其他任何东西,请告诉我。任何关于我做错了什么的建议都是值得赞赏的。 谢谢。

Package.h被多次包含。这会导致编译器多次看到 Package 类的定义,这是第一个错误消息的来源。

此文件中需要包含保护以防止多个包含:

#ifndef PACKAGE_H
#define PACKAGE_H
// Contents of your Package.h file
#endif

作为旁注,没有理由#include <iostream> Package.h. 此外,您不应该将using namespace std;放在头文件中,我强烈建议您避免完全使用它。

最新更新