User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,605 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,492 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 681 | Replies: 5 | Solved
Reply
Join Date: Oct 2007
Posts: 32
Reputation: tnvkrishna is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
tnvkrishna's Avatar
tnvkrishna tnvkrishna is offline Offline
Light Poster

Question problem with including files

  #1  
Nov 9th, 2007
hello,

i have a header file (intl.h)which contains the declaration of members of class intl
and the members functions are defined in 5 different headers

and i have one more header (floatl.h) which contains the total declaration of class floatl
and it's member functions

the over view is something like this

  1. //this is intl.h
  2.  
  3. # ifndef _header_i
  4. #define _header_i
  5.  
  6. #include "floatl.h"
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. class intl
  11. {
  12. ..
  13. ..
  14. };
  15. #endif

  1. //this is float_l.h
  2. #ifndef _header_f
  3. #define _header_f
  4.  
  5. #include "intl.h"
  6. #include <cstring>
  7. using namespace std;
  8.  
  9. class float
  10. {
  11. ..
  12. ..
  13. };
  14. //all methods defined here

and methods of intl class are declared in separate headers
as i said and include no header files

now i write a header allheaders.h which include
all the header files i wrote

and in a program i included this allheaders.h file
and want to use the class floatl which inturn uses class intl
(floatl and intl have no relation at all in terms of oop concept)

when i compiled my final program compilation error was
intl doen not name a type
was there any mistake in including files?
thanks in advance
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: problem with including files

  #2  
Nov 9th, 2007
> functions are defined in 5 different headers
what do these look like?

> a header allheaders.h which include all the header files
in what order?
Reply With Quote  
Join Date: Oct 2007
Posts: 32
Reputation: tnvkrishna is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
tnvkrishna's Avatar
tnvkrishna tnvkrishna is offline Offline
Light Poster

Re: problem with including files

  #3  
Nov 9th, 2007
all the header files contain friend operators definitions
and order in allheaders.h was

intl.h
intl_mem-1
inrl-mem-2
...
..
floatl.h

i think i got what the logical error was as i am typing this !
intl were using floatl
floatl members were using intl

how to solve this !
because neither class definition was complete before calling other other class ,
but that class uses this class
Last edited by tnvkrishna : Nov 9th, 2007 at 10:33 am.
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: problem with including files

  #4  
Nov 9th, 2007
> intl were using floatl floatl members were using intl how to solve this !
to declare the floatl members using intl, a declaration of intl would suffice.
to define them, a definition of intl is required.

so do things in this order:
a. declare the classes first.
b. define the classes (which have declarations of their members)
c. define the members.
for example:
//********* header b.h ***************
#ifndef _B_H_INCLUDED_
#define _B_H_INCLUDED_
struct A ;
// A has been declared; we can now declare (not yet define) functions using A
struct B
{
  inline int b_fun( const A& a ) ;
  int bm ;
  A* a ;
  friend inline int b_friend( B& b, const A& a ) ;
};
#endif // _B_H_INCLUDED_
//////////////////////////////////////////////////////////////////

//********* header a.h ***************
#ifndef _A_H_INCLUDED_
#define _A_H_INCLUDED_
struct B ;

struct A
{
  inline int a_fun( const B& b ) ;
  int am ;
  friend inline int a_friend( A& a, const B& b ) ;
};
#endif // _A_H_INCLUDED_
/////////////////////////////////////////////////////////////////////

//********* header anything_else.h ***************
#ifndef _ANYTHING_ELSE_H_INCLUDED_
#define _ANYTHING_ELSE_H_INCLUDED_

#include "a.h"
#include "b.h"
// A and B have been defined; we can now define functions using A and B

int A::a_fun( const B& b ) { return am += b.bm ; }
int B::b_fun( const A& a ) { return bm += a.am ; }
int a_friend( A& a, const B& b ) { return a.am += b.bm ; }
int b_friend( B& b, const A& a ) { return b.bm += a.am ; }
#endif // _ANYTHING_ELSE_H_INCLUDED_
Reply With Quote  
Join Date: Oct 2007
Posts: 32
Reputation: tnvkrishna is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
tnvkrishna's Avatar
tnvkrishna tnvkrishna is offline Offline
Light Poster

Question Re: problem with including files

  #5  
Nov 10th, 2007
this is a strange problem!

i declared class floatl(not defined)int the header intl.h
and declared class intl(not defined)floatl in the header floatl.h

and the error was strange!

power_i.h: In function ‘intl power(intl, int)’:
power_i.h:21: error: invalid use of undefined type ‘struct floatl’
intl.h:4: error: forward declaration of ‘struct floatl’
power_i.h:21: error: invalid use of undefined type ‘struct floatl’
intl.h:4: error: forward declaration of ‘struct floatl’

why is th ecompiler reading class declaration as struct declaration?
Last edited by tnvkrishna : Nov 10th, 2007 at 4:19 am.
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: problem with including files

  #6  
Nov 10th, 2007
> why is the compiler reading class declaration as struct declaration?
the compiler makes no distinction between a class or a struct in the declaration of the type. when the type is defined, the only difference between the two is the default access specifier.
so struct A { int i ; double d ; }; and
class A { public: int i ; double d ; }; are identical definitions.
also, class C {/*....*/}; class D : public C {/*...*/};
is identical to struct C {private: /*...*/}; class D : C {/*...*/};

for example, the following code will compile without errors:
  1. struct A ; class A ; struct A ;
  2. int foobar( A arg1, A& arg2 ) ; // ok, use in name
  3. //int foobar( A arg1, A& arg2 ) { return arg2.a = arg1.a ; };
  4. // uncomment the above and you get the error '.... undefined type ....'
  5. class A
  6. {
  7. public : int a ;
  8. };
  9. struct A ; class A ; struct A ;
  10. int foobar( A arg1, A& arg2 ) { return arg2.a = arg1.a ; }; // ok
  11.  
  12. class B ;
  13.  
  14. struct B : public A
  15. {
  16. private : int b ;
  17. public : operator int() const { return a + b ; }
  18. };
  19.  
  20. int main()
  21. {
  22. struct A a1 ;
  23. class A a2 ;
  24. class B b1 ;
  25. struct B b2 ;
  26. return a1.a + a2.a + b1.a + b2 ;
  27. }
> power_i.h:21: error: invalid use of undefined type ‘struct floatl
clearly means that
a. floatl has already been declared
b. floatl has not yet been defined
c. at line 21 in power_i.h, you are trying to use floati (not just in name, but in size)

> this is a strange problem!
no, it is not. it is a very commom problem that occurs when you try to use something
without having defined it.
Last edited by vijayan121 : Nov 10th, 2007 at 11:04 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:08 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC