Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,605 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 926 people online right now. Registration is fast and FREE... Join Now!




Using #include<some header file> -- Inheritance

2 Pages V  1 2 >  
Reply to this topicStart new topic

Using #include<some header file> -- Inheritance, Get error messages when trying to build

chaoticabyss99
post 15 Jul, 2008 - 11:31 PM
Post #1


D.I.C Head

**
Joined: 14 Jun, 2008
Posts: 92


My Contributions


I'm trying to implement Inheritance by having 1 base class and 2 derived classes. I have 3 files: 1 file for the base class, 1 file for the 2 derived classes, and 1 file for the Test file. 1st of all, am I doing this correctly: My base class is Package.h, then my 2nd file for my 2 derived classes is called TwoDayPackage.cpp, then my 3rd file, Test File, is called TwoDayPackageTest.cpp.

In TwoDayPackage.cpp, I put #include <Package.h>, then in TwoDayPackageTest.cpp, I put #include <TwoDayPackage.cpp>.

I am getting error messages that say:

fatal error C1083: Cannot open include file: 'Package.h': No such file or directory

fatal error C1083: Cannot open include file: 'TwoDayPackage.cpp': No such file or directory


What am I doing wrong here?

Please advise.
User is offlineProfile CardPM

Go to the top of the page

nirvanarupali
post 15 Jul, 2008 - 11:54 PM
Post #2


D.I.C Foot

Group Icon
Joined: 1 Aug, 2007
Posts: 983



Thanked 2 times

Dream Kudos: 375
My Contributions


Are these in the the same directory in main.cpp file? If so...

use this

#include <"Package.h" > instead of #include <Package.h>
User is offlineProfile CardPM

Go to the top of the page

chaoticabyss99
post 16 Jul, 2008 - 12:00 AM
Post #3


D.I.C Head

**
Joined: 14 Jun, 2008
Posts: 92


My Contributions


QUOTE(nirvanarupali @ 16 Jul, 2008 - 12:54 AM) *

Are these in the the same directory in main.cpp file? If so...

use this

#include <"Package.h" > instead of #include <Package.h>



I don't know exactly what you mean by
QUOTE
Are these in the same directory in main,cpp file?


However, I tried changing it to #include <"Package.h"> and it gave me an error message:

fatal error C1083: Cannot open include file: '"Package.cpp"': Invalid argument

fatal error C1083: Cannot open include file: '"Package.h"': Invalid argument
User is offlineProfile CardPM

Go to the top of the page

nirvanarupali
post 16 Jul, 2008 - 12:10 AM
Post #4


D.I.C Foot

Group Icon
Joined: 1 Aug, 2007
Posts: 983



Thanked 2 times

Dream Kudos: 375
My Contributions


Sorry, it should be : #include "Package.h"

It should be in the directory where the main cpp file is located. Or you have to put that in the include folder of your compiler and use this :
#include <Package.h>
User is offlineProfile CardPM

Go to the top of the page

chaoticabyss99
post 16 Jul, 2008 - 12:17 AM
Post #5


D.I.C Head

**
Joined: 14 Jun, 2008
Posts: 92


My Contributions


QUOTE(nirvanarupali @ 16 Jul, 2008 - 01:10 AM) *

Sorry, it should be : #include "Package.h"

It should be in the directory where the main cpp file is located. Or you have to put that in the include folder of your compiler and use this :
#include <Package.h>



OK, I tried that and now I have 15 errors. Would it help if I gave you all of the code?
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 16 Jul, 2008 - 03:05 AM
Post #6


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


It would probably help if you told us what errors you're getting. Not just that you got 15... And having source on hand might help too, see if we can't spot the errors, and help you fix them.
User is offlineProfile CardPM

Go to the top of the page

chaoticabyss99
post 16 Jul, 2008 - 10:32 AM
Post #7


D.I.C Head

**
Joined: 14 Jun, 2008
Posts: 92


My Contributions


Ok, here is what I have:

Package.h

CODE

#ifndef Package_H
#define Package_H

#include <iostream>

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); //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;  
};

#endif


Package.cpp

CODE

#include <iostream>

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)  
{  
    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

CODE

#include <iostream>

using namespace std;

#include "Package.cpp"
#include <conio.h>

//Test File
  
int main()  
{  
    OverNightPackage box("John Doe", "789 Fire Street", "Hell", "MI", "48169", "Jane Doe", "987 Legs Crossing", "Intercourse", "PA", "17534", 10.00, 1.50, .85);  
      
    TwoDayPackage parcel("John Doe", "789 Fire Street", "Hell", "MI", "48169", "Jane Doe", "987 Legs Crossing", "Intercourse", "PA", "17534", 15.00, 1.05, 5.00);  
  
    cout << fixed << setprecision(2);  
      
    cout << "To ship a box with overnight delivery:\n";  
    cout << "The sender    " << box.getSenderName()<< "\n";  
    cout << "              " << box.getSenderAdd() << "\n";  
    cout << "              " << box.getSenderCity() << ", " << box.getSenderState() << "  " << box.getSenderZip() << "\n";  
      
  
    cout << "The recipient " << box.getRecipientName()<< "\n";  
    cout << "              " << box.getRecipientAddress() << "\n";  
    cout << "              " << box.getRecipientCity() << ", " << box.getRecipientState() << "  " << box.getRecipientZip() << "\n";  
    cout << "The cost is   $ " <<box.calculateCost() << "\n\n";  
      
    cout << "\n\n\n";  
  
    cout << "To ship a parcel with 2 day delivery:\n";  
    cout << "The sender    " << parcel.getSenderName()<< "\n";  
    cout << "              " << parcel.getSenderAddress() << "\n";  
    cout << "              " << parcel.getSenderCity() << ", " << parcel.getSenderState() << "  " << parcel.getSenderZip() << "\n";  
      
  
    cout << "The recipient " << parcel.getRecipientName()<< "\n";  
    cout << "              " << parcel.getRecipientAddress() << "\n";  
    cout << "              " << parcel.getRecipientCity() << ", " << parcel.getRecipientState() << "  " << parcel.getRecipientZip() << "\n";  
    cout << "The cost is   $ "<<parcel.calculateCost() << "\n\n";  

    _getch();

}



I tried posting the list of error messages, but it wouldn't allow me for some reason. It kept giving me an error message. Weird.
User is offlineProfile CardPM

Go to the top of the page

chaoticabyss99
post 16 Jul, 2008 - 11:29 AM
Post #8


D.I.C Head

**
Joined: 14 Jun, 2008
Posts: 92


My Contributions


I think this is the main error message I keep getting multiple times:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

and I think it has to do with this piece

CODE

cout << "The sender    " << box.getSenderName()<< "\n";  
    cout << "              " << box.getSenderAddress() << "\n";  
    cout << "              " << box.getSenderCity() << ", " << box.getSenderState() << "  " << box.getSenderZip() << "\n";  
      
  
    cout << "The recipient " << box.getRecipientName()<< "\n";  
    cout << "              " << box.getRecipientAddress() << "\n";  
    cout << "              " << box.getRecipientCity() << ", " << box.getRecipientState() << "  " << box.getRecipientZip() << "\n";  
    cout << "The cost is   $ " <<box.calculateCost() << "\n\n";  
      
    cout << "\n\n\n";  
  
    cout << "To ship a parcel with 2 day delivery:\n";  
    cout << "The sender    " << parcel.getSenderName()<< "\n";  
    cout << "              " << parcel.getSenderAddress() << "\n";  
    cout << "              " << parcel.getSenderCity() << ", " << parcel.getSenderState() << "  " << parcel.getSenderZip() << "\n";  
      
  
    cout << "The recipient " << parcel.getRecipientName()<< "\n";  
    cout << "              " << parcel.getRecipientAddress() << "\n";  
    cout << "              " << parcel.getRecipientCity() << ", " << parcel.getRecipientState() << "  " << parcel.getRecipientZip() << "\n";  
    cout << "The cost is   $ "<<parcel.calculateCost() << "\n\n";  



What exactly does this error message mean?
User is offlineProfile CardPM

Go to the top of the page

chaoticabyss99
post 16 Jul, 2008 - 12:07 PM
Post #9


D.I.C Head

**
Joined: 14 Jun, 2008
Posts: 92


My Contributions


OK, I've got it down to 4 errors, which is still the:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)


Here is my updated Test File:

CODE

#include <iostream>
using namespace std;

#include <iomanip>
using std::setprecision;

#include "Package.cpp"
#include <conio.h>

//Test File
  
int main()  
{  
    OverNightPackage box("John Doe", "789 Fire Street", "Hell", "MI", "48169", "Jane Doe", "987 Legs Crossing", "Intercourse", "PA", "17534", 10.00, 1.50, .85);  
      
    TwoDayPackage parcel("John Doe", "789 Fire Street", "Hell", "MI", "48169", "Jane Doe", "987 Legs 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() << endl;  
      
  
    cout << "\nThe recipient   " << box.getRecipientName()  
         << "\n                " << box.getRecipientAddress()
         << "\n                " << box.getRecipientCity() << ", " << box.getRecipientState() << "  " << box.getRecipientZip()  
         << "\nThe cost is   $ " <<box.calculateCost() << endl;  
      
      
    cout << "\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() << endl;  
      
  
    cout << "\nThe recipient " << parcel.getRecipientName()
         << "\n              " << parcel.getRecipientAddress()
         << "\n              " << parcel.getRecipientCity() << ", " << parcel.getRecipientState() << "  " << parcel.getRecipientZip()
         << "\nThe cost is   $ "<<parcel.calculateCost() << endl;  

    _getch();

}



The 4 errors that are occurring have something to do with these lines:

CODE


<< "\n              " << box.getSenderAddress()

<< "\n                " << box.getRecipientAddress()

<< "\n              " << parcel.getSenderAddress()

<< "\n              " << parcel.getRecipientAddress()



what is wrong with these lines?
User is offlineProfile CardPM

Go to the top of the page

sergio1
post 16 Jul, 2008 - 01:01 PM
Post #10


New D.I.C Head

*
Joined: 15 Jul, 2008
Posts: 30


My Contributions


i think you forgot the ";" (semi colon) at the end.

This post has been edited by sergio1: 16 Jul, 2008 - 01:01 PM
User is offlineProfile CardPM

Go to the top of the page

Einherjar
post 16 Jul, 2008 - 06:19 PM
Post #11


D.I.C Head

**
Joined: 10 Feb, 2008
Posts: 73


My Contributions


Also you should be including "Package.h" and not .cpp

edit: It might work either way? I've never tried doing it that way in C++

This post has been edited by Einherjar: 16 Jul, 2008 - 06:20 PM
User is offlineProfile CardPM

Go to the top of the page

chaoticabyss99
post 16 Jul, 2008 - 09:35 PM
Post #12


D.I.C Head

**
Joined: 14 Jun, 2008
Posts: 92


My Contributions


QUOTE(Einherjar @ 16 Jul, 2008 - 07:19 PM) *

Also you should be including "Package.h" and not .cpp

edit: It might work either way? I've never tried doing it that way in C++



I tried removing
CODE
#include "Package.cpp"
and replaced it with
CODE
#include "Package.h"
, however, I got 50 errors!! So, I just included them both, and now I'm back to the same 4 errors.

I don't get it. mad.gif I have been using this example in my book as a reference, and although it doesn't consist of 2 derived classes, it's still fairly similar and I feel like this program should be working but it's not.

Does anyone know a good solution for this, please??
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/23/08 02:22AM