c++ - 我的 C++ 程序中有 3 个(我相信是相关的)错误

标签 c++ visual-studio

我的代码收到以下错误:

Error 3 error LNK1120: 1 unresolved externals c:\users\toking\documents\visual studio 2012\Projects\11.9\Debug\11.9.exe 11.9

Error 2 error LNK2001: unresolved external symbol "public: double __thiscall Package::calculateCost(float,double)" (?calculateCost@Package@@QAENMN@Z) c:\Users\toking\documents\visual studio 2012\Projects\11.9\11.9\11.9_main.obj 11.9

Error 1 error LNK2019: unresolved external symbol "public: double __thiscall Package::calculateCost(float,double)" (?calculateCost@Package@@QAENMN@Z) referenced in function "public: double __thiscall overnightPackage::calcCostOvernight(float,double,double)" (?calcCostOvernight@overnightPackage@@QAENMNN@Z) c:\Users\toking\documents\visual studio 2012\Projects\11.9\11.9\11.9.obj 11.9

这是我 C++ 的第二个学期,也是我第一次使用继承。我尝试以类似于我的书中调用它们的方式调用函数,但所有错误似乎都与我的派生类函数有关。任何帮助是极大的赞赏。

//主要

#include <iostream>
#include "stdafx.h"
#include <string>
using namespace std;

int main()
{
  int i;
  string customerName,customerAddress,city,state,senderAddress,recipientAddress;
  float packageWeight;
  string customerCity;
  double costPerOunce;
  double flatFee;
  double additionalCost;
  string customerState;
  int customerZipcode;
  Package base;
  twoDayPackage twoday;
  overnightPackage overnight;
  cout<<" *****Welcome To The American Package Delievery Services*****"<<endl<<endl;
  cout<<"Please Fill In The Requested Data Follow: "<<endl<<"-----------------------------------------"<<endl<<endl;;
  cout<<"Enter Customer Name "<<endl<<endl;
  cin>>customerName;
  cout<<endl;
  base.setName(customerName);
  cout<<"Enter Customer Address"<<endl<<endl;
  cin>>customerAddress;
  cout<<endl;
  base.setAddress(customerAddress);
  cout<<"Enter Customer City"<<endl<<endl;
  cin>>customerCity;
  cout<<endl;
  base.setCity(customerCity);
  cout<<"Enter Customer State"<<endl<<endl;
  cin>>customerState;
  cout<<endl;
  base.setState(customerState);
  cout<<"Enter Customer ZIP code"<<endl<<endl;
  cin>>customerZipcode;
  cout<<endl;
  base.setZip(customerZipcode);
  cout<<"Enter Weight"<<endl;
  cin>>packageWeight;
  cout<<endl;
  cout<<"Enter Cost Per Ounce"<<endl;
  cin>>costPerOunce;
  cout<<endl;
  cout<<"Please Enter Your Choice From The Menu Below:"<<endl<<endl;
  cout<<" 1- Calculate Base Cost "<<endl<<endl;
  cout<<" 2- Calculate Two Day Cost "<<endl<<endl;
  cout<<" 3- Calculate Over Night Cost"<<endl<<endl;
  cin>>i;
  cout<<endl;
  switch (i)
  {
    case 1 :
            base.calculateCost(packageWeight,costPerOunce);
            break;
    case 2 :
            cout<<"Enter Flat Cost"<<endl<<endl;
            cin >> flatFee;
            twoday.calcShippingCost(packageWeight,costPerOunce,flatFee);
            break;
    case 3 :
            cout<<"Enter The Additional Cost"<<endl<<endl;
            cin >> additionalCost;
            overnight.calcCostOvernight(packageWeight,costPerOunce,additionalCost);
            break;
    default:
            cout << "INVALID CHOICE....Please Enter ur Choice Number From 1-->3 "<<endl;
  }
  cout<<"Enter sender address "<<endl<<endl;
  cin>>senderAddress;
  cout<<endl;
  base.setSender( senderAddress);
  cout<<"Enter ricipent address"<<endl<<endl;
  cin>>recipientAddress;
  cout<<endl;
  base.setRecipient(recipientAddress);
  cout<<"address from:"<< senderAddress<<endl;
  cout<<"To:"<<recipientAddress<<endl;
  system ("pause");
return 0;
}

//标题

#include <iostream>
#include <string>
using namespace std;
class Package // Base Class
{
private:
  string name, city, state, sender, recipient;
  int zip;
  string address;
  float weight;
  double cost;
public:
  void setName(string);
  string getName();
  void setCity(string);
  string getCity();
  void setState(string);
  string getState();
  void setZip(int);
  int getZip();
  void setAddress(string);
  string getAddress();
  void setSender(string);
  string getSender();
  void setRecipient(string);
  string getRecipient();
  double calculateCost(float , double);
};


class twoDayPackage: public Package
{
public:
  double calcShippingCost(float, double, double);
private:
  double flatFee;
};

class overnightPackage: public Package
{
public:
  double calcCostOvernight(float, double, double);
private:
  double overnightCost;
};

//Cpp

#include <iostream>
#include "stdafx.h"
#include <string>
using namespace std;

void Package::setName(string n)
{
  name = n;
}
void Package::setCity(string c)
{
  city = c;
}
void Package::setState(string s)
{
  state = s;
}
void Package::setZip (int zp)
{
  zip = zp;
}
void Package::setAddress(string adr)
{
  address = adr;
}
void Package::setSender(string sen)
{
  sender = sen;
}
void Package::setRecipient(string rec)
{
  recipient = rec;
}
string Package::getName()
{
return name;
}
string Package::getCity()
{
return city;
}
string Package::getState()
{
return state;
}
int Package::getZip()
{
return zip;
}
string Package::getAddress()
{
return address;
}
string Package::getSender()
{
return sender;
}
string Package::getRecipient()
{
return recipient;
}
double calculateCost(float weight,double costPerOunce)
{
  double z;
  z = weight * costPerOunce;
  cout<< "The Base Cost = " <<z << endl<<endl;
return z;
}

double twoDayPackage::calcShippingCost(float weight, double costPerOunce, double flatFee)
{
  double z;
  z = calculateCost(weight,costPerOunce) + flatFee;
  cout << "The TwoDayPackage Cost = " << z << endl;
  return z;
}

double overnightPackage::calcCostOvernight(float weight,double costPerOunce,double additionalCost )
{
  double z;
  z = calculateCost(weight, costPerOunce)+(additionalCost * weight);
  cout<< "The OvernightPackage Cost = " <<z << endl;
  return z;
}

最佳答案

您的代码无法决定calculateCost 是否是Package 的成员函数。选择一种方式并坚持下去。

关于c++ - 我的 C++ 程序中有 3 个(我相信是相关的)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22083632/

相关文章:

c++ - USN中的文件引用号返回空

c++ - 检测两个类是否是兄弟

c++ - 加入线程时的性能问题

c# - Visual Studio : cannot access resources from code

c++ - VS2010 RC - 调试器中只有 100 个 std::map 元素

visual-studio - TFS 结帐命令行错误 9009

c++ - 重写这个循环

c++ - 如何选择数字到字符串转换的最小缓冲区大小?

c++ - 尝试在 Visual Studio 2013 中使用 sqlite3_open 进行编译时出错

visual-studio - NetStandard.Library NuGet 包和项目属性中的目标框架之间有什么关系?