Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Class B using Class C, and Class A using both B and C

Reply
Thread Tools

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

 
 
jd
Guest
Posts: n/a
 
      11-26-2004
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:rint_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:rint_value(){
std::cout << x << " " << y << " " << z << "\n";
}


 
Reply With Quote
 
 
 
 
Matthias =?ISO-8859-1?Q?K=E4ppler?=
Guest
Posts: n/a
 
      11-26-2004
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:rint_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:rint_value(){
> std::cout << x << " " << y << " " << z << "\n";
> }


 
Reply With Quote
 
 
 
 
Marcelo Pinto
Guest
Posts: n/a
 
      11-26-2004
jd <(E-Mail Removed)> wrote in message news:<co6k7v$9rv$(E-Mail Removed)>...
> 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:rint_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:rint_value(){
> std::cout << x << " " << y << " " << z << "\n";
> }


The modifications should correct the problem you are facing.

Good luck,

Marcelo Pinto
 
Reply With Quote
 
jd
Guest
Posts: n/a
 
      11-27-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compilation error: file exists in both in both 'c:\WINNT\Microsoft.NET\Framework\v2.0.50727 ABCL ASP .Net 0 05-29-2008 04:59 PM
2 computers, both online, both invisible to each other ? b Computer Support 9 04-21-2006 04:06 AM
How to config the web.config for both Internal and External users using Windows and Forms Authentication? ABC ASP .Net Security 1 10-31-2005 03:59 AM
How to config the web.config for both Internal and External users using Windows and Forms Authentication? ABC ASP .Net 1 10-24-2005 01:37 PM
Why support for both int.class and Integer.TYPE? Brian J. Sayatovic Java 1 09-05-2003 08:32 AM



Advertisments