0
#include <iostream>
#include <string>
#include <fstream>
#include <ios>
using namespace std;

static int numofPPackages=0;
static int numofOPackages=0;
static int numofTPackages=0;

class Persons {
public:
string Name;
string Address;
string City;
string State;
string Zip;

Persons ();
/*Persons (string name, string address, string city, string state, string zip) {
    Name=name; Address=address; City=city; State=state; Zip=zip;}*/
void fillpers (string name, string address, string city, string state, string zip)           {
    Name=name; Address=address; City=city; State=state; Zip=zip;}
friend ostream &operator<<(ostream &out, Persons);
string getName(){return Name;}
string getAddress(){return Address;}
string getCity(){return City;}
string getState(){return State;}
string getZip(){return Zip;}


};

class Package {
public:
int Weight;
char Ptype;
double Cost;
Persons Sender;
Persons Reciver;

Package(){}
/*Package(char ptype, int weight, double cost, Persons sender,Persons reciver){
    Ptype=ptype; Weight=weight; Cost=cost; Sender=sender; Reciver=reciver;}*/
void fillpak(char ptype, int weight, double cost, Persons sender,Persons reciver)
    {Ptype=ptype; Weight=weight; Cost=cost; Sender=sender; Reciver=reciver;}
static void printpak(Package toprint){
    cout<<"Package #"<<numofPPackages;
    cout<<"Shipper"<<endl<<toprint.Sender;
    cout<<"Reciver"<<endl<<toprint.Reciver;
    cout<<"Shipping cost for "<<toprint.Weight<<" ounces @"     <<toprint.Cost<<"/ounce is "<<toprint.Weight*toprint.Cost;
}
};

class OvernightPackage : public Package {
public:
double addcost;
//void printpak;
double Flat_rate_increase(){
    double addcost;
    ifstream readin;
    readin>>addcost;
    return addcost;
}
static void printOpak(Package toprint, double extcost){
    cout<<"Package #"<<numofOPackages;
    cout<<"Shipper"<<endl<<toprint.Sender;
    cout<<"Reciver"<<endl<<toprint.Reciver;
    cout<<"Shipping cost for "<<toprint.Weight<<" ounces @"<<toprint.Cost<<"/ounce + a flat rate of" << extcost<<" is "<<toprint.Weight*toprint.Cost+extcost;
}
};

class TwoDayPackage : public Package{
public:
double addcost;
//void printpak;
double Cost_per_ounce(int weight,double addcost){
    ifstream readin;
    readin>>addcost;
    return weight*addcost;
}
static void printTpak(Package toprint, double extcost){
    cout<<"Package #"<<numofTPackages;
    cout<<"Shipper"<<endl<<toprint.Sender;
    cout<<"Reciver"<<endl<<toprint.Reciver;
    cout<<"Shipping cost for "<<(toprint.Weight+extcost)<<" ounces @"<<toprint.Cost<<"/ounce is "<<(toprint.Weight+extcost)*toprint.Cost;
}
};

//overload for output
ostream &operator<<(ostream & out, Persons aperson)
{
out <<"Name: "<< aperson.getName()<<endl<<"Address: "<<aperson.getAddress()    <<endl<<"City, State Zip: "<<aperson.getCity()<<aperson.getState()<<aperson.getZip()<<endl;
return out;
}

int main() {
string filename;
int realfile=0;
int inWeight;
char inPtype;
double inCost;
string Name1, Address1, City1, State1, Zip1, Name2, Address2, City2, State2, Zip2;
double inaddcost;
Persons Pers1;
Persons Pers2;

Package stdpak;
TwoDayPackage tdpak;
OvernightPackage onpak;


while (realfile==0) 
{
    cout<<"Enter the file name you wish to pull data from"<<endl;
    cin>>filename;
    ifstream readin(filename+".txt");
    if (readin.is_open()){
        realfile++;
            while (!readin.eof()){

                readin>>inPtype;
                readin>>inWeight;               
                readin>>inCost;
                readin>>Name1;
                readin>>Address1;
                readin>>City1;
                readin>>State1;
                readin>>Zip1;
                readin>>Name2;
                readin>>Address2;
                readin>>City2;
                readin>>State2;
                readin>>Zip2;

                Pers1.fillpers(Name1, Address1, City1, State1, Zip1);
                Pers2.fillpers(Name2, Address2, City2, State2, Zip2);

                if (inPtype=='o'||inPtype=='O'){
                    readin>>inaddcost;
                    onpak.fillpak(inPtype, inWeight, inCost, Pers1, Pers2);
                    numofOPackages++;
                    OvernightPackage::printOpak(onpak,inaddcost);
                }
                if (inPtype=='t'||inPtype=='T'){
                    readin>>inaddcost;
                    tdpak.fillpak(inPtype, inWeight, inCost, Pers1, Pers2);
                    numofTPackages++;
                    TwoDayPackage::printTpak(tdpak,inaddcost);
                }
                else{
                    stdpak.fillpak(inPtype, inWeight, inCost, Pers1, Pers2);
                    numofPPackages++;
                    Package::printpak(stdpak);
                }
            }
        }
        else{cout<<"Try another file name."<<endl;}
    }



return 0;
}

该程序将从文件中获取输入,然后将该信息打印到屏幕上。所以我不断收到此错误-(错误 1 ​​错误 LNK2019:未解析的外部符号“public:__thiscall Persons::Persons(void)”(??0Persons@@QAE@XZ) 在函数_main 中引用)有人可以帮忙吗?

4

1 回答 1

0

因为没有默认构造函数的实现 -

Persons ();

您尝试使用它创建对象:

Persons Pers1;
Persons Pers2;

还有一些提示:

  • const通过引用而不是值传递类类型。
  • const在您可以标记的地方标记成员
  • 缩进代码
  • 放弃using namespace std;
  • 避免全局变量
  • 使数据成员private
  • 作文是一种has-a关系——aPackage没有2 个人。
  • 为什么static void printpak(Package toprint)?为什么不使用不带参数的常规参数?
  • 考虑virtual在派生类中覆盖一个单独的打印方法。
于 2013-02-22T22:43:33.613 回答