Compiler errors 获取此错误LNK2019,但不知道原因 #包括 #包括 #包括 #包括 使用名称空间std; 静态int numoppackages=0; 静态整数numfopackages=0; 静态int numotpackages=0; 阶级人士{ 公众: 字符串名; 字符串地址; 字符串城市; 字符串状态; 拉链; 人(); /*人员(字符串名称、字符串地址、字符串城市、字符串州、字符串邮政编码){ Name=Name;Address=Address;City=City;State=State;Zip=Zip;}*/ 空填充器(字符串名称、字符串地址、字符串城市、字符串状态、字符串zip){ Name=Name;Address=Address;City=City;State=State;Zip=Zip;} friend ostream&operator
,因为没有默认构造函数的实现-Compiler errors 获取此错误LNK2019,但不知道原因 #包括 #包括 #包括 #包括 使用名称空间std; 静态int numoppackages=0; 静态整数numfopackages=0; 静态int numotpackages=0; 阶级人士{ 公众: 字符串名; 字符串地址; 字符串城市; 字符串状态; 拉链; 人(); /*人员(字符串名称、字符串地址、字符串城市、字符串州、字符串邮政编码){ Name=Name;Address=Address;City=City;State=State;Zip=Zip;}*/ 空填充器(字符串名称、字符串地址、字符串城市、字符串状态、字符串zip){ Name=Name;Address=Address;City=City;State=State;Zip=Zip;} friend ostream&operator,compiler-errors,linker-errors,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: stri
#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;
}
并尝试使用它创建对象:
Persons ();
更多提示:
- 通过
引用而不是值传递类类型const
- 在可以的位置标记成员
const
- 缩进代码
- 使用命名空间std;
- 避免全球化
- 将数据成员设置为私有
- 合成是一种
关系-a关系-a
没有两个人包装
- 为什么
?为什么不使用不带参数的常规打印包静态void printpak(Package toprint)
- 考虑使用一个在派生类中重写的
print方法virtual
Persons Pers1;
Persons Pers2;