Class B using Class C, and Class A using both B and C

Discussion in 'C++' started by jd, Nov 26, 2004.

  1. jd

    jd Guest

    The following code illustrates a problem I have been trying to get around in
    c++. Can anyone explain how I can get this to work. The code compiles
    fine if I leave out the lines with the comments after them.

    Tnaks in advance for any help.
    jd

    //file test.cpp:
    #include "pair.h"
    #include "triple.h"

    int main(){
    pair y= pair(2,4);
    triple z = triple (1,2,3);
    pair *x= new pair(1,2);
    triple *fg= new triple (4,5,6);

    y.print_value();
    z.print_value();
    x->print_value();
    fg->print_value();

    return 0;
    }


    //file pair.h:
    class pair {
    private:
    int x, y;
    public:
    pair(int first, int second){
    x=first; y=second;
    }
    void print_value();
    };


    //file pair.cpp
    #include <iostream>
    #include "pair.h"

    void pair::print_value(){
    std::cout << x << " " << y << "\n";
    }


    //file triple.h
    #include "pair.h" //if I leave this and next line out, the program compiles

    class triple {
    private:
    int x, y, z;
    pair *mypair;// and leave this out

    /* also if possible I'd like to be able to make the previous line work
    without using a pointer as well as with. Now it works with neither unless
    I leave out the line completely.*/

    public:
    triple(int first, int second, int third){
    x=first; y=second; z=third;
    }

    void print_value();
    };

    //file triple.cpp
    #include <iostream>
    #include "triple.h"

    void triple::print_value(){
    std::cout << x << " " << y << " " << z << "\n";
    }
     
    jd, Nov 26, 2004
    #1
    1. Advertising

  2. 1) It would be a lot more helpful if you would tell us what the compilation
    error is.

    2) In general, you should avoid including your own headers into other
    headers where you can. It leads to a chain of dependencies and can make
    compilation of larger programs take very long and maybe even lead to cyclic
    inclusion (you don't want that).

    There are several solutions to avoid doing that:
    First, use forward declarations. In your case this is the right solution.
    Triple only holds a pointer to a pair object, that means, at compile time
    the compiler doesn't need to know ANYTHING of the implementation details of
    pair. So you don't need to give him the definition of pair.
    LIke this:

    class pair; //forward declaration, no #include

    class triple
    {
    ...
    pair * mypair;
    ...
    };

    Another solution would be to simply write pair in the same header or make it
    an inner class of triple, but only do so if pair is exclusively used for
    composing a triple. If it's needed completely elsewhere, this is not the
    right approach. I don't know your design so you have to decide yourself.

    Hope that helps,
    Matthias

    jd wrote:

    > The following code illustrates a problem I have been trying to get around
    > in
    > c++. Can anyone explain how I can get this to work. The code compiles
    > fine if I leave out the lines with the comments after them.
    >
    > Tnaks in advance for any help.
    > jd
    >
    > //file test.cpp:
    > #include "pair.h"
    > #include "triple.h"
    >
    > int main(){
    > pair y= pair(2,4);
    > triple z = triple (1,2,3);
    > pair *x= new pair(1,2);
    > triple *fg= new triple (4,5,6);
    >
    > y.print_value();
    > z.print_value();
    > x->print_value();
    > fg->print_value();
    >
    > return 0;
    > }
    >
    >
    > //file pair.h:
    > class pair {
    > private:
    > int x, y;
    > public:
    > pair(int first, int second){
    > x=first; y=second;
    > }
    > void print_value();
    > };
    >
    >
    > //file pair.cpp
    > #include <iostream>
    > #include "pair.h"
    >
    > void pair::print_value(){
    > std::cout << x << " " << y << "\n";
    > }
    >
    >
    > //file triple.h
    > #include "pair.h" //if I leave this and next line out, the program
    > #compiles
    >
    > class triple {
    > private:
    > int x, y, z;
    > pair *mypair;// and leave this out
    >
    > /* also if possible I'd like to be able to make the previous line work
    > without using a pointer as well as with. Now it works with neither unless
    > I leave out the line completely.*/
    >
    > public:
    > triple(int first, int second, int third){
    > x=first; y=second; z=third;
    > }
    >
    > void print_value();
    > };
    >
    > //file triple.cpp
    > #include <iostream>
    > #include "triple.h"
    >
    > void triple::print_value(){
    > std::cout << x << " " << y << " " << z << "\n";
    > }
     
    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Nov 26, 2004
    #2
    1. Advertising

  3. jd <> wrote in message news:<co6k7v$9rv$>...
    > The following code illustrates a problem I have been trying to get around in
    > c++. Can anyone explain how I can get this to work. The code compiles
    > fine if I leave out the lines with the comments after them.
    >
    > Tnaks in advance for any help.
    > jd
    >
    > //file test.cpp:
    > #include "pair.h"
    > #include "triple.h"
    >
    > int main(){
    > pair y= pair(2,4);
    > triple z = triple (1,2,3);
    > pair *x= new pair(1,2);
    > triple *fg= new triple (4,5,6);
    >
    > y.print_value();
    > z.print_value();
    > x->print_value();
    > fg->print_value();
    >
    > return 0;
    > }
    >
    >
    > //file pair.h:
    > class pair {
    > private:
    > int x, y;
    > public:
    > pair(int first, int second){
    > x=first; y=second;
    > }
    > void print_value();
    > };
    >

    You should use include guards:

    >
    > //file pair.cpp



    #ifndef PAIR_H_YOUR_NAME
    #define PAIR_H_YOUR_NAME
    > #include <iostream>
    > #include "pair.h"
    >
    > void pair::print_value(){
    > std::cout << x << " " << y << "\n";
    > }
    >

    #endif


    >
    > //file triple.h



    #ifndef TRIPLE_H_YOUR_NAME
    #define TRIPLE_H_YOUR_NAME
    > #include "pair.h" //if I leave this and next line out, the program compiles
    >
    > class triple {
    > private:
    > int x, y, z;
    > pair *mypair;// and leave this out
    >
    > /* also if possible I'd like to be able to make the previous line work
    > without using a pointer as well as with. Now it works with neither unless
    > I leave out the line completely.*/
    >
    > public:
    > triple(int first, int second, int third){
    > x=first; y=second; z=third;
    > }
    >
    > void print_value();
    > };
    >

    #endif


    > //file triple.cpp
    > #include <iostream>
    > #include "triple.h"
    >
    > void triple::print_value(){
    > std::cout << x << " " << y << " " << z << "\n";
    > }


    The modifications should correct the problem you are facing.

    Good luck,

    Marcelo Pinto
     
    Marcelo Pinto, Nov 26, 2004
    #3
  4. jd

    jd Guest

    That has helped a lot thanks.
    jd

    Matthias K├Ąppler wrote:

    > 1) It would be a lot more helpful if you would tell us what the
    > compilation error is.
    >
    > 2) In general, you should avoid including your own headers into other
    > headers where you can. It leads to a chain of dependencies and can make
    > compilation of larger programs take very long and maybe even lead to
    > cyclic inclusion (you don't want that).
    >
    > There are several solutions to avoid doing that:
    > First, use forward declarations. In your case this is the right solution.
    > Triple only holds a pointer to a pair object, that means, at compile time
    > the compiler doesn't need to know ANYTHING of the implementation details
    > of pair. So you don't need to give him the definition of pair.
    > LIke this:
    >
    > class pair; //forward declaration, no #include
    >
    > class triple
    > {
    > ...
    > pair * mypair;
    > ...
    > };
    >
    > Another solution would be to simply write pair in the same header or make
    > it an inner class of triple, but only do so if pair is exclusively used
    > for composing a triple. If it's needed completely elsewhere, this is not
    > the right approach. I don't know your design so you have to decide
    > yourself.
    >
    > Hope that helps,
    > Matthias
     
    jd, Nov 27, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ABC
    Replies:
    1
    Views:
    802
    Richard Dudley
    Oct 24, 2005
  2. Brian J. Sayatovic
    Replies:
    1
    Views:
    392
    Chris Uppal
    Sep 5, 2003
  3. Mike Schilling
    Replies:
    10
    Views:
    718
    Mike Schilling
    Sep 29, 2003
  4. ABCL
    Replies:
    0
    Views:
    563
  5. ABC
    Replies:
    1
    Views:
    367
    Patrick.O.Ige
    Oct 31, 2005
Loading...

Share This Page