得到这个错误LNK2019,不知道为什么

时间:2013-02-22 22:41:34

标签: compiler-errors linker-errors

#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中引用) 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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

Persons ();

您尝试使用它创建对象:

Persons Pers1;
Persons Pers2;

更多提示:

  • const引用而不是值传递类类型。
  • 标记成员const,您可以
  • 缩进代码
  • 抛弃using namespace std;
  • 避免全局
  • 制作数据成员private
  • 组成是一种has-a关系 - Package没有 有两个人。
  • 为什么static void printpak(Package toprint)?为什么不是不带参数的常规?
  • 考虑使用单个virtual打印方法,在派生类时将其覆盖。