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_val ue(){
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_v alue(){
std::cout << x << " " << y << " " << z << "\n";
} 3 1248
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_val ue(){ 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_v alue(){ std::cout << x << " " << y << " " << z << "\n"; }
jd <jd@noemail.com > wrote in message news:<co******* ***@domitilla.a ioe.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_NAM E
#define PAIR_H_YOUR_NAM E #include <iostream> #include "pair.h"
void pair::print_val ue(){ std::cout << x << " " << y << "\n"; }
#endif //file triple.h
#ifndef TRIPLE_H_YOUR_N AME
#define TRIPLE_H_YOUR_N AME #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_v alue(){ std::cout << x << " " << y << " " << z << "\n"; }
The modifications should correct the problem you are facing.
Good luck,
Marcelo Pinto
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 resolve the warnings and errors.
#include <iostream>
using namespace std;
|
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 A
{
std::vector<B*> Bs;
public:
|
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 the class and this works but I would
also like to know of other ways to do this.
Also are there any performance implacations of using sealed?
|
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 maxMudLvl;
|
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 of time for any help or feedback.
Cheers,
Peter
| |
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 or another (could also be 3) different web servers to
use these Web Services. The Web Services are identical on all the machines.
I tried just changing the URL of the Web Services and cannot make it work.
I then decided to create 2 identical web...
|
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 this, I want to use
it. Is it possible to call Managed class functions from Unmanaged
class? How to do it?
I did something like this.
I declared a managed class (in C++ CLI) called as MyManagedClass whose
|
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 thawed it out. I built a console app using
Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the
header file had to be commented out.
I built a console app using Borland C++ Builder 5. The linker complained of
references to...
|
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 name search scope.
i.e. a very simple application would be
class ManyComputations
{
calling System.Math;
|
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 delegates.
The first way is an adaptation from C++ for Dummies by Jeff Cogswell.
But obviously it uses references not pointers.
The second way (I'll post this later, as I haven't yet done it) will
be adapted from the book C#3.0 Design Patterns by...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |