Welcome to Dream.In.Code
Become a C++ Expert!

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




Seperate file compilation

 
Reply to this topicStart new topic

Seperate file compilation

captainhampton
2 Feb, 2008 - 10:21 AM
Post #1

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
Hey all, I am attempting to compile and run this code from two different files, my cpp file which works all fine and dandy when compiled is no problem, and a .h file

Here is the regular code which works fine:

CODE


#include <cstdlib>
#include <iostream>
#include <cctype>

using namespace std;


class Pairs
{
      friend Pairs operator+(const Pairs x, const Pairs y);
      friend Pairs operator*(const Pairs& x, const Pairs& y);

      
    public:
        Pairs();
        Pairs(int first, int second);
        friend istream& operator >> (istream& ins, Pairs& second);
        friend ostream& operator << (ostream& outs, const Pairs& second);
    private:
        int f;
        int s;
};

Pairs::Pairs()                                                                  //Default Constructor
{
}

Pairs::Pairs(int first, int second)                                             //Constructor W/ Parameters
{
                
}

Pairs operator+(const Pairs x, const Pairs y)                                   //Overloaded "+" Function
{
    Pairs P;

    P.f = x.f + y.f;                                                            //Achieves The ( a + c ) Result
    P.s = x.s + y.s;                                                            //Achieves The ( b + d ) Result

    return P;
}

Pairs operator*(const Pairs& x, const Pairs& y)                                 //Overloaded "*" Function
{
    Pairs P;

    P.f = x.f * y.f;                                                            //Achieves The ( a * c ) Result
    P.s = x.s * y.f;                                                            //Achieves The ( b * c ) Result

    return P;
}

istream& operator >> (istream& ins, Pairs& second)                              //Overloaded ">>" Function
{
         cout << "First Number" <<endl;
         ins >> second.f;
        
         cout << "Second Number" <<endl;
         ins >> second.s;
        
         return ins;
}

ostream& operator << (ostream& outs, const Pairs& second)                       //Overloaded "<<" Function
{
         cout << "Output Numbers:" <<endl;
         outs << "(" << second.f << "," << second.s <<  ")" <<endl;
        
         return outs;
}


int main(int argc, char *argv[])
{
    Pairs first_pair, second_pair;
    
    cout << "First Pair:"<<endl;
    cin >> first_pair;                                                          //Takes In First Pair Of Numbers
    
    cout << "Second Pair:" <<endl;
    cin >> second_pair;                                                         //Takes In Second Pair Of Numbers

    cout << "The first pair of numbers is: " <<endl;
    cout << first_pair <<endl <<endl;
  
    cout << "The second pair of numbers is: " <<endl;
    cout << second_pair <<endl <<endl;
  
    cout << "The pair after addition is as follows: "<<endl;
    cout << first_pair + second_pair <<endl <<endl;                             //Displays The Proper Addition Of Pairs
    
    cout << "The pair after multiplication is as follows: "<<endl;              //Displays The Proper Multiplication Of Pairs
    cout << first_pair * second_pair <<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}


That all works fine, so I attempt to take out the class def and save it as a .h file


(.h file)

CODE


#include <cstdlib>
#include <iostream>
#include <cctype>

using namespace std;



class Pairs
{
      friend Pairs operator+(const Pairs x, const Pairs y);
      friend Pairs operator*(const Pairs& x, const Pairs& y);

      
    public:
        Pairs();
        Pairs(int first, int second);
        friend istream& operator >> (istream& ins, Pairs& second);
        friend ostream& operator << (ostream& outs, const Pairs& second);
    private:
        int f;
        int s;
};



I save the .h file as "Pairs.h" which I include in the cpp file as: #include"Pairs.h" However it doesn't seem like it's able to find the file.

Any help would be appreciated.

User is offlineProfile CardPM
+Quote Post

GWatt
RE: Seperate File Compilation
2 Feb, 2008 - 11:14 AM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,359



Thanked: 31 times
Dream Kudos: 500
My Contributions
Can you paste the exact code for the file which includes Pairs.h?
User is online!Profile CardPM
+Quote Post

Tom9729
RE: Seperate File Compilation
2 Feb, 2008 - 01:28 PM
Post #3

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,592



Thanked: 12 times
Dream Kudos: 325
My Contributions
Add #include "Pairs.h" to the top of your "Pairs.cpp" file, and remove the declaration of the Pairs class from the cpp file.

After that, it compiles fine for me. If you're it's having trouble finding the header file, make sure everything is the same case.
User is online!Profile CardPM
+Quote Post

captainhampton
RE: Seperate File Compilation
3 Feb, 2008 - 10:02 AM
Post #4

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
It does compile fine however it does not run, I get a linker error winmain@16.
User is offlineProfile CardPM
+Quote Post

captainhampton
RE: Seperate File Compilation
3 Feb, 2008 - 10:21 AM
Post #5

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
Actually nvm, my compiler is just flaky...Sorry and thanks for the help everyone
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 10:23PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month