2014-02-22 125 views
-2

我一直在研究這個代碼,在我的OvernightPackage.h文件中,一直在爭取一個小小的錯誤。 OvernightPackage繼承自超類Package,但它一直拋出錯誤:「預期的類名」我還有另一個類TwoDayPackage,它繼承了很好,我無法弄清楚他們兩個之間有什麼不同。Eclipse C++繼承問題

繼承人我的代碼:

的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";}; 
+4

TL; DR!請修改您的問題以包含*完整的*和*未經編輯的*錯誤日誌。然後將代碼縮小到錯誤中引用的行以及某些上下文。 –

+0

Boooah !!! TL; DR; ...爲什麼[標籤:eclpse]順便說一句。 –

回答

1

你的問題很可能是由於缺少包括。在繼承的類中需要包含基類,所以將

#include "Package.h" 

添加到兩個繼承類中。另外,Package.h需要一個包含Person.h的文件,其中幾個文件應包含<string>,因爲您使用它。

順便說一句,在你的示例代碼中,你發佈了person.cpp,其中package.cpp應該是,而且整個代碼要長。下次發帖之前請仔細閱讀如何做Short, Self Contained, Correct (Compilable), Example

+0

感謝您的建議,我是新的stackoverflow。對不起,我缺乏足夠的知識。 – Ronin