Eclipse C ++继承问题

时间:2014-02-22 21:45:58

标签: c++ inheritance

我一直在使用这个代码在我的OvernightPackage.h文件中处理一个小错误。 OvernightPackage继承自超类Package,但它不断抛出错误:“期望的类名”我还有另一个类TwoDayPackage,它继承得很好,我无法弄清楚它们两者之间有什么不同。

继承我的代码:

的main.cpp

//============================================================================
// Name        : Assignment4.cpp
//============================================================================

#include <iostream>
#include <fstream>
#include <string>
#include "Person.h"
#include "Package.h"
#include "TwoDayPackage.h"
#include "OvernightPackage.h"



void readFile(char & c, int & i, double & d, double & ed, string attributes[]){
    ifstream input;
    input.open("package.txt");
    input >> c;
    input >> i;
    input >> d;
    for(int i = 0; i < 10; i++){
        input >> attributes[i];
    }
    switch(c){
    case('o'):
    case('O'):
    case('t'):
    case('T'):
        input >> ed;
        break;
    }

}

void printLabel(Package item){

    cout << "********* Shipping Label *******" << endl << endl;
    cout << "Delivery Cost: " << item.costToDeliver() << endl;
    cout << "The package will arrive at its destination in " << item.timeToDeliver() << " days.";
    cout << "From:\n";
    cout << item.getSenderName() << endl;
    cout << item.getSenderAddress() << endl;
    cout << item.getSenderCity() << "," << item.getSenderState() << " " << item.getSenderZip() << endl ;
    cout << "Ship To:\n";
    cout << item.getRecieverName() << endl;
    cout << item.getRecieverAddress() << endl;
    cout << item.getRecieverCity() << "," << item.getRecieverState() << " " << item.getRecieverZip() << endl ;
    cout << "\n******************************";


}

int main() {
    char packageType;
    int weight;
    double costPerOunce, extraData;
    string attributes[10];
    readFile(packageType, weight, costPerOunce, extraData, attributes);
    Person sender = Person(attributes[0],attributes[1],attributes[2],attributes[3],attributes[4]);
    Person receiver = Person(attributes[5],attributes[6],attributes[7],attributes[8],attributes[9]);
    Package package;

    switch(packageType){
    case('p'):
    case('P'):
        package = Package(weight,costPerOunce);
        break;
    case('o'):
    case('O'):
        package = OvernightPackage(weight,costPerOunce,extraData);
        break;
    case('t'):
    case('T'):
        package = TwoDayPackage(weight,costPerOunce,extraData);
        break;
    }
    package.setSender(sender);
    package.setReceiver(receiver);
    printLabel(package);

}

Person.h

/*
 * Person.h
 *
 *  Created on: Feb 20, 2014
 *      Author: Nate Ashby
 */

#ifndef PERSON_H_
#define PERSON_H_

using namespace std;

class Person{
public:
    Person();
    Person(string name, string address, string city, string state, string zip);
    string getName();
    string getAddress();
    string getCity();
    string getState();
    string getZip();
private:
    string name;
    string address;
    string city;
    string state;
    string zip;
};



#endif /* PERSON_H_ */

Person.cpp

/*
 * Person.cpp
 *
 *  Created on: Feb 20, 2014
 *      Author: Nate Ashby
 */
#include "Person.h"

using namespace std;

Person::Person(){
    name = "Empty";
    address = "Empty";
    city = "Empty";
    state = "Empty";
    zip = "Empty";
};

Person::Person(string name, string address, string city, string state, string zip){
    this->name = name;
    this->address = address;
    this->city = city;
    this->state = state;
    this->zip = zip;


};
string Person::getName(){return name;};
string Person::getAddress(){return address;};
string Person::getCity(){return city;};
string Person::getState(){return state;};
string Person::getZip(){return zip;};

Package.h

/*
 * Package.h
 *
 *  Created on: Feb 20, 2014
 *      Author: Nate Ashby
 */

#ifndef PACKAGE_H_
#define PACKAGE_H_


class Package{
public:
    Package();
    Package(int ounces, double costPerOunce);
    int getWeight();
    double getCostPerOunce();
    Person getSender();
    Person getReciever();
    void setWeight(int);
    void setCostPerOunce(double);
    void setSender(Person);
    void setReceiver(Person);
    double costToDeliver();
    std::string timeToDeliver();
    friend class Person;
    std::string getSenderName();
    std::string getSenderAddress();
    std::string getSenderCity();
    std::string getSenderState();
    std::string getSenderZip();
    std::string getRecieverName();
    std::string getRecieverAddress();
    std::string getRecieverCity();
    std::string getRecieverState();
    std::string getRecieverZip();
protected:
    int ounces;
    double costPerOunce;
    Person Sender;
    Person Reciever;
};

Package.cpp

/*
 * Person.cpp
 *
 *  Created on: Feb 20, 2014
 *      Author: Nate Ashby
 */
#include "Person.h"

using namespace std;

Person::Person(){
    name = "Empty";
    address = "Empty";
    city = "Empty";
    state = "Empty";
    zip = "Empty";
};

Person::Person(string name, string address, string city, string state, string zip){
    this->name = name;
    this->address = address;
    this->city = city;
    this->state = state;
    this->zip = zip;


};
string Person::getName(){return name;};
string Person::getAddress(){return address;};
string Person::getCity(){return city;};
string Person::getState(){return state;};
string Person::getZip(){return zip;};

TwoDayPackage.h

/*
 * TwoDayPackage.h
 *
 *  Created on: Feb 20, 2014
 *      Author: Nate Ashby
 */

#ifndef TWODAYPACKAGE_H_
#define TWODAYPACKAGE_H_


class TwoDayPackage : public Package{
public:
    TwoDayPackage();
    TwoDayPackage(int,double,double);
    void setIncrease(double);
    double getIncrease();
private:
    double costPerOunceIncrease;
};




#endif /* TWODAYPACKAGE_H_ */

TwoDayPackage.cpp

/*
 * TwoDayPackage.cpp
 *
 *  Created on: Feb 20, 2014
 *      Author: Nate Ashby
 */
#include "TwoDayPackage.h"

TwoDayPackage::TwoDayPackage(){
    ounces = 0;
    costPerOunce = 0;
    costPerOunceIncrease = 0;
};
TwoDayPackage::TwoDayPackage(int weight, double cost, double rateIncrease){
    ounces = weight;
    costPerOunce = cost;
    costPerOunceIncrease = rateIncrease;
};
void TwoDayPackage::setIncrease(double rateIncrease){ costPerOunceIncrease = rateIncrease;};
double TwoDayPackage::getIncrease(){return costPerOunceIncrease;};
double TwoDayPackage::costToDeliver(){return (ounces * (costPerOunce + costPerOunceIncrease));};
std::string timeToDeliver(){return "2";};

OvernightPackage.h

/*
 * OvernightPackage.h
 *
 *  Created on: Feb 20, 2014
 *      Author: Nate Ashby
 */

#ifndef OVERNIGHTPACKAGE_H_
#define OVERNIGHTPACKAGE_H_


class OvernightPackage : public Package{
public:
    OvernightPackage();
    OvernightPackage(int, double, double);
    double getRate();
    void setRate(double);
protected:
    double flatRateIncrease;
};


#endif /* OVERNIGHTPACKAGE_H_ */

最后是OvernightPackage.cpp

/*
 * OvernightPackage.cpp
 *
 *  Created on: Feb 20, 2014
 *      Author: Nate Ashby
 */

#include "OvernightPackage.h"

OvernightPackage::OvernightPackage(){
    ounces = 0;
    costPerOunce = 0;
    flatRateIncrease = 0;
}
OvernightPackage::OvernightPackage(int weight, double costPerOunce, double overnightIncrease){
    this->ounces = weight;
    this->costPerOunce = costPerOunce;
    this->flatRateIncrease = overnightIncrease;
}
double OvernightPackage::getRate(){return flatRateIncrease;};
void OvernightPackage::setRate(double rate){flatRateIncrease = rate;};
double OvernightPackage::costToDeliver(){ return (ounces * costPerOunce + flatRateIncrease);};
std::string timeToDeliver(){return "1";};

1 个答案:

答案 0 :(得分:1)

您的问题很可能是由于缺少包含。在继承的类中,您需要包含基类,因此添加

#include "Package.h" 

到两个继承类。此外,Package.h需要Person.h的包含,并且自您使用以来,其中一些文件应包含<string>

顺便说一下,在你的示例代码中,你发布了person.cpp应该是package.cpp的,而整个代码是长的。在下次发布之前,请阅读有关如何Short, Self Contained, Correct (Compilable), Example

的信息