GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Mar-2008, 14:39
cpit cpit is offline
Junior Member
 
Join Date: Jan 2006
Posts: 52
cpit is on a distinguished road
Unhappy

A header file do not recognize another


Hi all,

I'm trying to create a method that receive an object as an argument. I get it working when the two classes are defined in the same file.h. However, I would like to know how to do that with two files.h.

For instance, this is my code that works,

CPP / C++ / C Code:
//file classes.h

class Cvalues{
		
	public:
	int x,y;
	void set_values(int,int);
	void get_values();	
};

class Ccalc{
	int x,y;
	
	public:
	int sum(int, int);
	int sum(Cvalues&);
};

Now, I would like to write a class in each file (classes1.h, classes2.h)

CPP / C++ / C Code:
//file classes1.h

class Cvalues{
		
	public:
	int x,y;
	void set_values(int,int);
	void get_values();	
};

and

CPP / C++ / C Code:

//file classes2.h
class Ccalc{
	int x,y;
	
	public:
	int sum(int, int);
	int sum(Cvalues&); <-- Here is my dude!
};

How can I do to classes2.h to recognize the type Cvalues of classes1.h?

Thank you.
  #2  
Old 21-Mar-2008, 15:02
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 395
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: A header file do not recognize another


Quote:
Originally Posted by cpit
How can I do to classes2.h to recognize the type Cvalues of classes1.h?
In classes2.h, put the line:
CPP / C++ / C Code:
#include "classes1.h"
at the top of the file.
  #3  
Old 22-Mar-2008, 22:00
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 353
Peter_APIIT is on a distinguished road

Re: A header file do not recognize another


You also can add a forward declaration if two classes are related to each other.

class a;
class b
{
}

if class b using object of class a and vice versa.

I hope this help
__________________
Linux is the best OS in the world.
  #4  
Old 28-Mar-2008, 05:16
sheree sheree is offline
New Member
 
Join Date: Mar 2008
Posts: 6
sheree is on a distinguished road

Re: A header file do not recognize another


I recommand fakepoo suggestion
it is the easiest solution
  #5  
Old 28-Mar-2008, 06:03
C++_Bandit's Avatar
C++_Bandit C++_Bandit is offline
Junior Member
 
Join Date: Mar 2008
Location: Virginia
Posts: 40
C++_Bandit will become famous soon enough

Re: A header file do not recognize another


Actually, fakepoo's solution is not only easier it is the standard way you include files that you have created in any program.

Also, don't forget to optimize your code using #ifndef, you don't need to make your x and y values public since you have a function to get those values(which needs to be int so you can return them. So your final code should look like:
CPP / C++ / C Code:
//file classes1.h
#ifndef CLASSES1.H
#define CLASSES1.H

class Cvalues
{
   int x,y;

public:
   void set_values(int, int);
   int get_values();
};

#endif
CPP / C++ / C Code:

//file classes2.h
#ifndef CLASSES2.H
#define CLASSES2.H

#include "classes1.h"

class Ccalc
{
   int x,y;

public:
   int sum (int, int);
   int sum (Cvalues&);
};

#endif

These suggestions should help when you try to debug your code. I hope it makes sense.
  #6  
Old 28-Mar-2008, 08:44
L7Sqr L7Sqr is offline
Junior Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 91
L7Sqr will become famous soon enough

Re: A header file do not recognize another


Be careful. Most times the #include solution will work, but consider the following:
CPP / C++ / C Code:
//File one.hpp
#ifndef ONE
#define ONE
#include "two.hpp"
class One {
   Two * two;
};
#endif


//File two.hpp
#ifndef TWO
#define TWO
#include "one.hpp"
class Two {
   One * one;
};
#endif
That will not compile; you will get 'no type' errors. However, if you change both of the files so that it is a forward declaration instead of an include
CPP / C++ / C Code:
//In File one.hpp
//Change #include "two.hpp" to
class Two;

//In file two.hpp
//Change #include "one.hpp" to
class One;
You will have no trouble. The caveat is that you now have to use pointers.
It may not be often that you run into such a situation, but it does happen. And when it does, it is nice to know that you have options.
 

Recent GIDBlogUpdates On The All New Toyota VIOS - Part III by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
After execution - Error cannot locate /Skin File? WSCH CPP / C++ Forum 1 05-Mar-2005 20:03
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 03:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:28.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.