0

I am receiving the following errors with my code:

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

This is my second semester of C++ and my first time using inheritance. I tried calling the functions similarly to how my book calls them, but all of the errors seem to be related to my derived classes functions. Any help is greatly appreciated.

// Main

#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;
}

// Header

#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;
}
  • 1
    http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – chris Feb 28 '14 at 00:17
  • I did some research prior to posting this(I've been at it for an hour or so) and found that article. If I'm reading it correctly, it says that I have an object being declared, but not used, or being used, but not declared. I'm not sure which case it is as I can't find either occurring. – user3348654 Feb 28 '14 at 00:21
  • Not declared, defined. – chris Feb 28 '14 at 00:23
  • I also did not see where for example function calculateCost is defined. Do you see yourself its definition in the code snippets that you presented?! – Vlad from Moscow Feb 28 '14 at 00:24
  • I updated my post, I accidentally posted my header twice. calculateCost is in my .cpp code. -Thanks – user3348654 Feb 28 '14 at 00:40

1 Answers1

1

Your code can't decide whether calculateCost is a member function of Package or not. Pick one way and stick to it.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • What exactly do you mean by pick one way? Do you mean put the function in my header under my class? I was told that function definitions don't belong in classes. – user3348654 Feb 28 '14 at 00:53
  • 1
    You seem confused. Let's start with something very simple -- do you want `calculateCost` to be a class member function of `Package`? Or do you want it to be a free function? – David Schwartz Feb 28 '14 at 00:57
  • I guess free function would make the most sense since I'm calling it out everywhere. – user3348654 Feb 28 '14 at 03:47
  • Then fix `base.calculateCost(packageWeight,costPerOunce);` and take `calculateCost` out of the `class` declaration. – David Schwartz Feb 28 '14 at 03:49
  • This got rid of all my errors. But when I put just calculateCost in my .cpp my main can't read it, and when I put it in my Main, my .cpp twoday and overnight can't read it. Any ideas? – user3348654 Mar 03 '14 at 15:46