By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,115 Members | 894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.