compiler-errors - 收到此错误LNK2019,不知道为什么

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?

(34个答案)


6年前关闭。



#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)”(?? Persons @@ QAE @ XZ)在函数_main中引用)
有人可以帮忙吗?

最佳答案

由于没有默认构造函数的实现-

Persons ();

您尝试使用它创建对象:
Persons Pers1;
Persons Pers2;

更多提示:
  • 通过const引用而不是值传递类类型。
  • 将成员const标记为可以在其中
  • 缩进代码
  • 抛弃using namespace std;
  • 避免全局变量
  • 使数据成员成为private
  • 的构成是has-a的关系-Package没有2个人。
  • 为什么选择static void printpak(Package toprint)?为什么不选择一个不带参数的常规呢?
  • 考虑在派生类中重写的单个virtual打印方法。
  • https://stackoverflow.com/questions/15034435/

    上一篇:python - AttributeError : 'int' object has no attribute 'state'

    下一篇:jquery - Eclipse不了解jQuery

    相关文章:

    java - 这是 “unreachable statement”怎么样?

    使用 Link inTime 替换的 C++ 单元测试

    c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

    c++ - 错误 LNK2019 错误 C++

    c++ - MSVC linker error LNK2019 when reusing a base class in C++ 解决方案

    c++ - 虚幻引擎4.21.2和Squareys CISQLite3插件编译问题

    c++ - 使用 minGW 的 Netbeans 在编译时无法识别系统

    c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

    java - 无法执行目标org.apache.maven.plugins :maven-compiler-plugin:3.1:compile (default-compile) on project [duplicate]

    scala - 需要帮助找出 Scala 编译器错误

    相关文章:

    java - Java : Longest Ascending substring

    apache - htaccess 将所有页面重定向到单个页面

    python - AttributeError : 'int' object has no attr

    c++ - 数组下标错误的类型 'int[int]'无效

    ios - iOS静态库取决于其他静态库

    c++ - 编译器错误 : Undefined symbols for architecture x

    ruby-on-rails - Rails ActionView::Template::Error:

    iphone - NSInvalidArgumentException与UITableView吗?

    compilation - 安装MPFR(3.1.0)时出错

    c++ - OpenCV无法编译