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

J

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";
}
 
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

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
 
M

Marcelo Pinto

jd said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top