Help on Multiple Friend Class Include Problems

Discussion in 'C++' started by windmill2008@gmail.com, Mar 23, 2007.

  1. Guest

    I am trying to write two classes which can access the same base class
    as shown in the following code. My problem is when I include these two
    classes in the main.cpp, errors pop up saying that the base class is
    redefined. Is there anyone who can tell me how to do with it? ( I
    still want to have two friend classes of the same baseclass)
    thanks
    ***********************************************************************************************
    baseclass.h:
    ---------------------------------------
    class baseClass
    {

    private:

    static int arrays[2][2];

    public:
    friend class newClass1;
    friend class newClass2;

    };
    ----------------------------------------
    newclass1.h:
    -----------------------------------------
    #include baseclass.h
    class newClass1
    {
    public:
    access(baseClass *);
    };

    --------------------------------------
    newclass2.h:
    --------------------------------------
    #include baseclass.h
    class newClass2
    {
    public:
    calcuate(baseClass *);
    };
    -----------------------------------------------

    #include "newclass1.h"
    #include "newclass2.h"

    Main.cpp:
    {
    newclass1 mynewclass1;
    newclass2 mynewclass2;
    ......
    }
     
    , Mar 23, 2007
    #1
    1. Advertising

  2. red floyd Guest

    wrote:
    > I am trying to write two classes which can access the same base class
    > as shown in the following code. My problem is when I include these two
    > classes in the main.cpp, errors pop up saying that the base class is
    > redefined. Is there anyone who can tell me how to do with it? ( I
    > still want to have two friend classes of the same baseclass)
    > thanks
    > ***********************************************************************************************
    > baseclass.h:
    > ---------------------------------------
    > class baseClass
    > {
    >
    > private:
    >
    > static int arrays[2][2];
    >
    > public:
    > friend class newClass1;
    > friend class newClass2;
    >
    > };
    > ----------------------------------------
    > newclass1.h:
    > -----------------------------------------
    > #include baseclass.h
    > class newClass1
    > {
    > public:
    > access(baseClass *);
    > };
    >
    > --------------------------------------
    > newclass2.h:
    > --------------------------------------
    > #include baseclass.h
    > class newClass2
    > {
    > public:
    > calcuate(baseClass *);
    > };
    > -----------------------------------------------
    >
    > #include "newclass1.h"
    > #include "newclass2.h"
    >
    > Main.cpp:
    > {
    > newclass1 mynewclass1;
    > newclass2 mynewclass2;
    > .....
    > }
    >


    You are including baseclass.h twice (note, in your h files, you need the
    quotes around baseclass.h). Google for "include guard".

    // baseclass.h
    #ifndef BASECLASS_H_
    #define BASECLASS_H_

    class baseClass { ... };

    #endif
     
    red floyd, Mar 23, 2007
    #2
    1. Advertising

  3. David Harmon Guest

    On 23 Mar 2007 11:02:47 -0700 in comp.lang.c++,
    wrote,
    >I am trying to write two classes which can access the same base class
    >as shown in the following code. My problem is when I include these two
    >classes in the main.cpp, errors pop up saying that the base class is
    >redefined.


    Most headers should be written in such a way that they can be included
    repeatedly without causing that problem. The usual solution is
    preprocessor "include guards" along the lines of:

    >baseclass.h:
    >---------------------------------------


    #ifndef BASECLASS_H
    #define BASECLASS_H

    >class baseClass
    >{
    >
    >private:
    >
    > static int arrays[2][2];
    >
    >public:
    > friend class newClass1;
    > friend class newClass2;
    >
    >};


    #endif
     
    David Harmon, Mar 23, 2007
    #3
  4. Guest

    Thanks a lot.
     
    , Mar 23, 2007
    #4
  5. Michael Guest

    > ----------------------------------------
    > newclass1.h:
    > -----------------------------------------
    > #include baseclass.h
    > class newClass1
    > {
    > public:
    > access(baseClass *);
    >
    > };


    In addition to the include guard advice, you don't need in #include
    baseclass.h in your .h files (in this case). Instead, you could
    simply forward declare baseClass:

    class baseClass; // Forward declaration only.
    class newClass1
    {
    public:
    access(baseClass *);

    };

    This may or may not matter for your current project, but it's a good
    habit to get into - don't include any files you don't absolutely have
    to, because for larger projects unnecessary includes add significant
    recompilation time whenever you make a change to the included files.

    Michael
     
    Michael, Mar 23, 2007
    #5
  6. windmill Guest

    thanks a lot

    James
     
    windmill, Mar 23, 2007
    #6
    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. Alex Vinokur
    Replies:
    1
    Views:
    379
    mlimber
    Jun 6, 2006
  2. Joseph Turian
    Replies:
    6
    Views:
    404
    John Carson
    Nov 21, 2006
  3. Replies:
    1
    Views:
    338
    Victor Bazarov
    Nov 22, 2006
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    1,001
    Andreas Bogenberger
    Feb 22, 2008
  5. Peter
    Replies:
    2
    Views:
    310
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page