473,414 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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

Mar 23 '07 #1
5 5973
wi**********@gmail.com 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
Mar 23 '07 #2
On 23 Mar 2007 11:02:47 -0700 in comp.lang.c++, wi**********@gmail.com
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

Mar 23 '07 #3
Thanks a lot.

Mar 23 '07 #4
----------------------------------------
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

Mar 23 '07 #5
thanks a lot

James

Mar 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Nevyn | last post by:
Hi, i've got a problem with the following declarations file list.h ----------- class listOfItem : public QPtrList<Item> {
1
by: Alex Borghgraef | last post by:
Hi all, I'm trying to get the Limp (Large Image Manipulation Program) library to compile using gcc 3.2, and I've encountered some problems. One is that the library systematically refers to...
3
by: MoCha | last post by:
hello everyone, i want to know of any trick used to restrict the access scope of a friend class (or function). ie instead of the friend class having access to the entire internals of the class in...
1
by: Joe Carner via .NET 247 | last post by:
First time posting, thanks in advance for any help you can give me. Basically I am trying to a class that i want to be able to access the private data members of another class, both of which i...
4
by: KriS | last post by:
Hi, I have two simple questions: 1) Do I have to write storage-class-specifier, when I want to declare a class as friend: class Bar {}; class Foo { friend ??? Bar; };
7
by: Gary Wessle | last post by:
Hi I need class A to access a method of class B using the friend mechanism. // B.h #include "a.h" class B {
1
by: Alan Johnson | last post by:
Consider the following piece of code: // test.cpp class B ; namespace N { class A { friend class B ;
7
by: Nike | last post by:
The following code doesn't compile. I have a requirement in my project to declare one template as a friend function to another.. Can anyone suggest a way???? #include <iostream.h> template...
4
by: Yarco | last post by:
In c++ we could modify the private variable in a class use friend keyword. How could it be done in php? class YourBag { int money; friend class Thief; // friend? };
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.