Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old March 23rd, 2007, 07:05 PM
windmill2008@gmail.com
Guest
 
Posts: n/a
Default Help on Multiple Friend Class Include Problems

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;
......
}

  #2  
Old March 23rd, 2007, 07:15 PM
red floyd
Guest
 
Posts: n/a
Default Re: Help on Multiple Friend Class Include Problems

windmill2008@gmail.com wrote:
Quote:
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
  #3  
Old March 23rd, 2007, 07:15 PM
David Harmon
Guest
 
Posts: n/a
Default Re: Help on Multiple Friend Class Include Problems

On 23 Mar 2007 11:02:47 -0700 in comp.lang.c++, windmill2008@gmail.com
wrote,
Quote:
>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:
Quote:
>baseclass.h:
>---------------------------------------
#ifndef BASECLASS_H
#define BASECLASS_H
Quote:
>class baseClass
>{
>
>private:
>
> static int arrays[2][2];
>
>public:
> friend class newClass1;
> friend class newClass2;
>
>};
#endif

  #4  
Old March 23rd, 2007, 07:25 PM
windmill2008@gmail.com
Guest
 
Posts: n/a
Default Re: Help on Multiple Friend Class Include Problems

Thanks a lot.

  #5  
Old March 23rd, 2007, 07:55 PM
Michael
Guest
 
Posts: n/a
Default Re: Help on Multiple Friend Class Include Problems

----------------------------------------
Quote:
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

  #6  
Old March 23rd, 2007, 08:45 PM
windmill
Guest
 
Posts: n/a
Default Re: Help on Multiple Friend Class Include Problems

thanks a lot

James

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.