473,513 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

jd
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";
}
Jul 22 '05 #1
3 1234
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";
}


Jul 22 '05 #2
jd <jd@noemail.com> wrote in message news:<co**********@domitilla.aioe.org>...
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
Jul 22 '05 #3
jd
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


Jul 22 '05 #4

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

Similar topics

2
2015
by: gg | last post by:
I am facing some problems with following program. I am using aCC version 03.27 on HP-UX11. The command line I use to compile is - aCC -AA -I. TestTempMethods.C Can anybody pls suggest how to...
21
4031
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
5
14407
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
8
1390
by: StillStuckOnJava | last post by:
I'm using visual studio to create a web application, but I'm having trouble using a few classes that I've defined: public class tank { public tank() { } public float depth; public float...
1
831
by: Peter Nofelt | last post by:
Hey All, I am having issue with serializing a class and its base class using ..net 2.0. I am using IXmlSerializable I've provided code displaying my at the bottom of this post. Thanks ahead...
16
2333
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
9
3100
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
3
3739
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
49
5761
by: Ben Voigt [C++ MVP] | last post by:
I'm trying to construct a compelling example of the need for a language feature, with full support for generics, to introduce all static members and nested classes of another type into the current...
9
2390
by: raylopez99 | last post by:
Here are two different ways of achieving a mediator pattern: the first, using circular references (for lack of a better way to describe it), but not using delegates, with the second using...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
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
7535
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
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5683
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,...
1
5085
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...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.