home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / gnu / g / help / 1715 < prev    next >
Encoding:
Text File  |  1993-01-28  |  3.5 KB  |  151 lines

  1. Path: sparky!uunet!UB.com!pacbell.com!sgiblab!spool.mu.edu!agate!doc.ic.ac.uk!warwick!uknet!axion!planet.bt.co.uk!naw
  2. From: naw@planet.bt.co.uk (Ab Wilson)
  3. Newsgroups: gnu.g++.help
  4. Subject: Re: Problem linking with g++ class
  5. Message-ID: <1993Jan28.113627.9938@planet.bt.co.uk>
  6. Date: 28 Jan 93 11:36:27 GMT
  7. References: <1k3jc2INNjnk@iskut.ucs.ubc.ca> <honglin.18@apmaths.uwo.ca>
  8. Sender: news@planet.bt.co.uk
  9. Reply-To: naw@planet.bt.co.uk
  10. Organization: BT Labs, Martlesham Heath, Ipswich, UK.
  11. Lines: 137
  12. In-Reply-To: honglin@apmaths.uwo.ca's message of 26 Jan 93 19:32:12 GMT
  13.  
  14.  
  15. I usually don't post answers to querries in this news group because
  16. mostly the questions are stupid. This is no exception but I was
  17. prompted to reply by the message:
  18.  
  19.   Hi,
  20.  
  21.      I don't think you can get an answer from this news group for that 
  22.  
  23.   problen. There have been many help requests on that matter in the group,
  24.  
  25.   but people simply ignored or maybe they don't know the answer yet.
  26.  
  27. Posted by  honglin@apmaths.uwo.ca (HONGLIN YE).
  28.  
  29. Ok. This is what you should have:
  30.  
  31. >>>test1.c:
  32. #include <iostream.h>
  33. #include <math.h>
  34. #include "mpf14.h"
  35.  
  36. mpfloat square(mpfloat a);
  37.  
  38. main() {
  39.   mpfloat a = 4;
  40.   mpfloat b = square(a);
  41.   return 0;
  42. }
  43. >>>test2.c
  44. #include "mpf14.h"
  45.  
  46. mpfloat square(mpfloat a) {
  47.  
  48.     return( a*a );
  49.  
  50. }
  51. >>>mpf14.c
  52. #include "mpf14.h"
  53. >>>mpf14.h
  54. #ifndef _mpfloat_h
  55. #ifdef __GNUG__
  56. #pragma interface
  57. #endif
  58. #define _mpfloat_h 1
  59.  
  60.  
  61. // INCLUDE FILES 
  62. #include <iostream.h>
  63. #include <math.h>
  64. #include <stdlib.h>
  65.  
  66.  
  67. class mpfloat {
  68.  
  69. protected:
  70.  
  71.     double num;
  72.  
  73. public:
  74.  
  75.     // constructors and destructors
  76.     mpfloat();
  77.     mpfloat(const mpfloat& x);
  78.     mpfloat(const double x);
  79.     ~mpfloat();
  80.  
  81.     // get value
  82.     double getnum() const;
  83.  
  84. };
  85.  
  86.  
  87. // INLINE FUNCTION PROTOTYPES
  88.  
  89. // Arithmetic operators
  90. mpfloat operator * ( const mpfloat& x, const mpfloat& y);
  91.  
  92. // INLINE MEMBERS
  93.  
  94. // constructors
  95. inline mpfloat::mpfloat() {
  96. }
  97. inline mpfloat::mpfloat(const mpfloat& x) :num(x.getnum()) {
  98. }
  99. inline mpfloat::mpfloat(const double x) {
  100.     num = x;
  101. }
  102. inline mpfloat::~mpfloat() {
  103. }
  104.  
  105.  
  106. // Get number
  107. inline double mpfloat::getnum() const {
  108.     return(num);
  109. }
  110.  
  111.  
  112. // Inline Functions
  113.  
  114. inline mpfloat operator * ( const mpfloat& x, const mpfloat& y) {
  115.     return mpfloat( x.getnum()*y.getnum() );
  116. }
  117.  
  118. #endif // _mpfloat_h
  119.  
  120.  
  121. The result:
  122.  
  123. 242 :-) g++ test1.c test2.c mpf14.c -lm
  124. 243 :-) ls
  125. a.out   mpf14.c mpf14.h test1.c test2.c
  126. 244 :-) a.out 
  127. 245 :-) 
  128.  
  129. Now for future reference, in G++, whenever you have a header file
  130. containing either inline function definitions, or classes with virtual
  131. functions (or derived from classes with virtual functions), you must
  132. ALWAYS have a corresponding .c file. 
  133.  
  134. Why? Because symbols will be generated from this file (either vtables,
  135. or bodies for the inline functions). Fine you say, but why don't the
  136. symbols end up in one of the src file the .h is included in? Ans:
  137. because you said #pragma interface. Why did you say #pragma interface?
  138. Because if you didn't, then G++ would generate symbols in ALL the
  139. src files that include the .h.  Whats wrong with that you ask? Ans: you
  140. get multiply defined symbols.  How do I prevent this you ask? Ans: you
  141. use #pragma interface. But then G++ won't generate the symbols so what
  142. do I do? Ans: RTFM, #pragma interface in file foo.h says "don't
  143. generate symbols for this file unless it is included in foo.c".
  144. Problem solved?
  145.     Ab.
  146.  
  147. -- 
  148. +--------------------------------------------------------------------+
  149. | Whose opinions did you say these were?    |    naw@planet.bt.co.uk |
  150. +--------------------------------------------------------------------+
  151.