home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18625 < prev    next >
Encoding:
Internet Message Format  |  1993-01-01  |  1.8 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!max.fiu.edu!serss0!feathers
  2. From: feathers@serss0 (Michael Feathers)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: More Gnu C++ fun
  5. Message-ID: <C06HCB.H4M@fiu.edu>
  6. Date: 1 Jan 93 13:55:23 GMT
  7. References: <1992Dec30.195714.9824@adimail.uucp>
  8. Sender: news@fiu.edu (Usenet Administrator)
  9. Organization: Florida International University, Miami
  10. Lines: 55
  11.  
  12. In article <1992Dec30.195714.9824@adimail.uucp> tel@adimail.uucp (Terry Monks) writes:
  13. >This minimal program
  14. >
  15. >
  16. >class node 
  17. >{
  18. >public:
  19. >  static int level;
  20. >  
  21. >  node(){};
  22. >};
  23. >
  24. >main()
  25. >{
  26. >  node::level=1;
  27. >}
  28. >
  29. >
  30. >compiles and links fine using the AT&T compiler from Sun, but will not link
  31. >under Gnu c++.  (sparc-sun-sunos.4.1)
  32. >
  33. >I get the message
  34. >ld: Undefined symbol
  35. >   __4node$level
  36. >
  37. >
  38. >Any clues?
  39. >
  40. >-- 
  41. >Terry Monks        Automata Design Inc    (703) 742-9400
  42.  
  43. I think that the problem may be that you don't have a definition of 
  44. node::level in your program.  It is declared within the class but
  45. technically, no data is allocated.
  46.  
  47. Imagine if it were.  Every file that you include your class declaration
  48. within would have a static instance of node::level.  Some implementations,
  49. I suppose, will create the single instance you want in one particular file
  50. behind the scenes, but many compilers force you to define all your class
  51. static variables outside class declarations.  In fact, I think the ARM
  52. requires this now.   
  53.  
  54. For your program, you can achieve the effect you want by placing the
  55. following line before main ():
  56.  
  57. int node::level = 1;
  58.  
  59. You can delete the initialization within main also.  Since node::level is
  60. truly static, you can give it that initial value in its definition.
  61.  
  62. Michael C. Feathers
  63.  
  64. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  65. My opinions are not those of any organization because organizations do
  66. not have opinions.
  67.