•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,588 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,661 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 238 | Replies: 17
![]() |
•
•
Join Date: Jul 2008
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
I'm having trouble with an inheritance hierarchy program. I have widdled this program down from a lot of errors to just 1 error. However, I can't seem to figure out how to correct it.
Here is the error message I'm receiving:
Here is what I have:
Package.h
Package.cpp
PackageTest.cpp
Does anyone know how to correct this issue?
Here is the error message I'm receiving:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
Here is what I have:
Package.h
#ifndef Package_H
#define Package_H
#include <iostream>
#include <string>
using namespace std;
//The class Package is the base class for derived classes TwoDayPackage and OverNightPackage
class Package //begins class Package
{
public:
Package(const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, const string &, double = 0.0, double = 0.0, double = 0.0); //constructor
//set and get functions for sender
void setSenderName(const string &);
string getSenderName() const;
void setSenderAddress(const string &);
string getSenderAddress() const;
void setSenderCity(const string &);
string getSenderCity() const;
void setSenderState(const string &);
string getSenderState() const;
void setSenderZip(const string &);
string getSenderZip() const;
//set and get functions for recipient
void setRecipientName(const string &);
string getRecipientName() const;
void setRecipientAddress(const string &);
string getRecipientAddress() const;
void setRecipientCity(const string &);
string getRecipientCity() const;
void setRecipientState(const string &);
string getRecipientState() const;
void setRecipientZip(const string &);
string getRecipientZip() const;
void setWeight(double);
double getWeight() const;
void setShip(double);
double getShip() const;
double calculateCost() const;
private:
string senderName;
string senderAddress;
string senderCity;
string senderState;
string senderZip;
string recipientName;
string recipientAddress;
string recipientCity;
string recipientState;
string recipientZip;
double weight;
double shipCost;
};
#endifPackage.cpp
#include <iostream>
#include <string>
using namespace std;
#include "Package.h"
Package::Package(const string & sname, const string & saddress, const string & scity, const string & sstate, const string & szip, const string & rname, const string & raddress, const string & rcity, const string & rstate, const string & rzip, double weight, double shipCost, double calculateCost)
{
senderName = sname;
senderAddress = saddress;
senderCity = scity;
senderState = sstate;
senderZip = szip;
recipientName = rname;
recipientAddress = raddress;
recipientCity = rcity;
recipientState = rstate;
recipientZip = rzip;
setWeight(weight);
setShip(shipCost);
}
void Package::setSenderName(const string & sname)
{
senderName = sname;
}
string Package::getSenderName() const
{
return senderName;
}
void Package::setSenderAddress(const string & saddress)
{
senderAddress = saddress;
}
string Package::getSenderAddress() const
{
return senderAddress;
}
void Package::setSenderCity(const string & scity)
{
senderCity = scity;
}
string Package::getSenderCity() const
{
return senderCity;
}
void Package::setSenderState(const string & sstate)
{
senderState = sstate;
}
string Package::getSenderState() const
{
return senderState;
}
void Package::setSenderZip(const string & szip)
{
senderZip = szip;
}
string Package::getSenderZip() const
{
return senderZip;
}
void Package::setRecipientName(const string & rname)
{
recipientName = rname;
}
string Package::getRecipientName() const
{
return recipientName;
}
void Package::setRecipientAddress(const string & raddress)
{
recipientAddress = raddress;
}
string Package::getRecipientAddress() const
{
return recipientAddress;
}
void Package::setRecipientCity(const string & rcity)
{
recipientCity = rcity;
}
string Package::getRecipientCity() const
{
return recipientCity;
}
void Package::setRecipientState(const string & rstate)
{
recipientState = rstate;
}
string Package::getRecipientState() const
{
return recipientState;
}
void Package::setRecipientZip(const string & rzip)
{
recipientZip = rzip;
}
string Package::getRecipientZip() const
{
return recipientZip;
}
void Package::setWeight(double weight)
{
weight = (weight < 0.0 ) ? 0.0 : weight;
}
double Package::getWeight() const
{
return weight;
}
void Package::setShip(double shipCost)
{
shipCost = ( shipCost < 0.0) ? 0.0 : shipCost;
}
double Package::getShip() const
{
return shipCost;
}
double Package::calculateCost() const
{
return weight * shipCost;
}
//The class TwoDayPackage is the first derived class from class Package
class TwoDayPackage : public Package
{
public:
TwoDayPackage(const string &, const string &, const string &, const string &, const string &, const string &,
const string &, const string &, const string &, const string &, double = 0.0, double = 0.0, double = 0.0); //constructor
void setFlatFee(double);
double getFlatFee() const;
void calculateCost() const;
private:
double flatFee;
};
//The class OverNightPackage is the second derived class from class Package
class OverNightPackage : public Package
{
public:
OverNightPackage(const string &, const string &, const string &, const string &, const string &, const string &,
const string &, const string &, const string &, const string &, double = 0.0, double = 0.0, double = 0.0); //constructor
void setFee(double);
double getFee() const;
void calculateCost() const;
private:
double fee;
}; PackageTest.cpp
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>
using namespace std;
using std::setprecision;
#include "Package.cpp"
//Test File
int main()
{
OverNightPackage box("John Doe", "789 Fire Street", "Hell", "MI", "48169", "Jane Doe", "987 Leg Sun Crossing", "Intercourse", "PA", "17534", 10.00, 1.50, .85);
TwoDayPackage parcel("John Doe", "789 Fire Street", "Hell", "MI", "48169", "Jane Doe", "987 Leg Sun Crossing", "Intercourse", "PA", "17534", 15.00, 1.05, 5.00);
cout << fixed << setprecision(2);
cout << "To ship a box with overnight delivery:\n"
<< "\nThe sender " << box.getSenderName()
<< "\n " << box.getSenderAddress()
<< "\n " << box.getSenderCity() << ", " << box.getSenderState() << " " << box.getSenderZip()
<< "\nThe recipient " << box.getRecipientName()
<< "\n " << box.getRecipientAddress()
<< "\n " << box.getRecipientCity() << ", " << box.getRecipientState() << " " << box.getRecipientZip()
<< "\nThe cost is $ " << box.calculateCost()
<< "\n\n\n\nTo ship a parcel with 2 day delivery:\n"
<< "\nThe sender " << parcel.getSenderName()
<< "\n " << parcel.getSenderAddress()
<< "\n " << parcel.getSenderCity() << ", " << parcel.getSenderState() << " " << parcel.getSenderZip()
<< "\nThe recipient " << parcel.getRecipientName()
<< "\n " << parcel.getRecipientAddress()
<< "\n " << parcel.getRecipientCity() << ", " << parcel.getRecipientState() << " " << parcel.getRecipientZip()
<< "\nThe cost is $ "<< parcel.calculateCost() << endl;
_getch();
}Does anyone know how to correct this issue?
•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,562
Reputation:
Rep Power: 8
Solved Threads: 158
This line:
[edit]Too slow .... what ^^ said.
<< "\nThe cost is $ " << box.calculateCost() is probably causing the problem. The function calculateCost() isn't returning anything (void). To solve the problem, you should delete the function declaration from TwoDayPackage and OverNightPackage. The class 'Package' already has a calculateCost-function, so why make 2 more?[edit]Too slow .... what ^^ said.
Last edited by niek_e : 33 Days Ago at 2:34 am.
do NOT pm me for help, it makes me angry. You wouldn't like me when I'm angry...
•
•
Join Date: Jul 2008
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
parcel.calculateCost() and box.calculateCost() seems to be the culprits. In the derived class the methods calculateCost returns 'void'.
When I change calculateCost to return 'double' I get 31 error messages. They are:
•
•
•
•
error LNK2005: "public: __thiscall Package:ackage(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,double,double,double)" (??0Package@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@000000000NNN@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: void __thiscall Package::setSenderName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setSenderName@Package@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Package::getSenderName(void)const " (?getSenderName@Package@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: void __thiscall Package::setSenderAddress(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setSenderAddress@Package@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Package::getSenderAddress(void)const " (?getSenderAddress@Package@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: void __thiscall Package::setSenderCity(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setSenderCity@Package@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Package::getSenderCity(void)const " (?getSenderCity@Package@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: void __thiscall Package::setSenderState(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setSenderState@Package@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Package::getSenderState(void)const " (?getSenderState@Package@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: void __thiscall Package::setSenderZip(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setSenderZip@Package@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Package::getSenderZip(void)const " (?getSenderZip@Package@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: void __thiscall Package::setRecipientName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setRecipientName@Package@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Package::getRecipientName(void)const " (?getRecipientName@Package@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: void __thiscall Package::setRecipientAddress(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setRecipientAddress@Package@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Package::getRecipientAddress(void)const " (?getRecipientAddress@Package@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: void __thiscall Package::setRecipientCity(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setRecipientCity@Package@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Package::getRecipientCity(void)const " (?getRecipientCity@Package@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: void __thiscall Package::setRecipientState(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setRecipientState@Package@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Package::getRecipientState(void)const " (?getRecipientState@Package@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: void __thiscall Package::setRecipientZip(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setRecipientZip@Package@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Package::getRecipientZip(void)const " (?getRecipientZip@Package@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: void __thiscall Package::setWeight(double)" (?setWeight@Package@@QAEXN@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: double __thiscall Package::getWeight(void)const " (?getWeight@Package@@QBENXZ) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: void __thiscall Package::setShip(double)" (?setShip@Package@@QAEXN@Z) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: double __thiscall Package::getShip(void)const " (?getShip@Package@@QBENXZ) already defined in Package.obj
1>PackageTest.obj : error LNK2005: "public: double __thiscall Package::calculateCost(void)const " (?calculateCost@Package@@QBENXZ) already defined in Package.obj
1>PackageTest.obj : error LNK2019: unresolved external symbol "public: double __thiscall OverNightPackage::calculateCost(void)" (?calculateCost@OverNightPackage@@QAENXZ) referenced in function _main
1>PackageTest.obj : error LNK2019: unresolved external symbol "public: double __thiscall TwoDayPackage::calculateCost(void)" (?calculateCost@TwoDayPackage@@QAENXZ) referenced in function _main
1>PackageTest.obj : error LNK2019: unresolved external symbol "public: __thiscall TwoDayPackage::TwoDayPackage(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,double,double,double)" (??0TwoDayPackage@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@000000000NNN@Z) referenced in function _main
1>PackageTest.obj : error LNK2019: unresolved external symbol "public: __thiscall OverNightPackage::OverNightPackage(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,double,double,double)" (??0OverNightPackage@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@000000000NNN@Z) referenced in function _main
•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,562
Reputation:
Rep Power: 8
Solved Threads: 158
•
•
Join Date: Jul 2008
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
This line:
<< "\nThe cost is $ " << box.calculateCost()is probably causing the problem. The function calculateCost() isn't returning anything (void). To solve the problem, you should delete the function declaration from TwoDayPackage and OverNightPackage. The class 'Package' already has a calculateCost-function, so why make 2 more?
[edit]Too slow .... what ^^ said.
When I deleted the function from declaration from TwoDayPackage and OverNightPackage, I get 29 errors
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Threading query (Java)
- casting from userData (C++)
- Please help (C++)
- Webservice (ASP.NET)
- Virtual constructor (C)
- Inheritance problems (Java)
- Inheritance Project .. (Game Development)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: Question about accounting program
- Next Thread: Constructor question



ackage(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,double,double,double)" (??0Package@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@000000000NNN@Z) already defined in Package.obj
Linear Mode