Re: linkage error when initializing static member array

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 10/23/04


Date: Sat, 23 Oct 2004 01:57:54 GMT


"Neno" <m_marchionni@tiscali.it> wrote...
> I have a linkage error that has something to do with the use of a static
> member array and I can't understand how to solve the problem. Can someone
> help me please?

Static data members have to be defined. You need to place the definition
at the namespace level where your class is declared.

> In detail:
> A class Month (it is just an unsafe and useless example) has protected
> constructors so that Month objects can be obtained only through a static
> member function named getMonth.
> Month class has a private static array named months made of 12 pointers to
> Month objects and I would like getMonths to populate the array when
> necessary and return just the objects in the array.
> The problem is that the linker complains with :
> unresolved external symbol "private: static class Month * *
> Month::months"
>
> The code below is the header and cpp files for the class Month.
>
> ----- Month.h -----------------------------
> class Month {
> public:
> virtual ~Month();
> static Month *getMonth(int num);
> const int numOfDays;
> protected:
> Month(int num);
> Month(Month const &copy);
> private:
> static Month *months[12];
> };
> ------ Month.cpp -----------------------------
> #include "Month.h"

Add here:

   Month *Month::months[12] = { 0 };

>
> Month::Month(int num) : numOfDays(num) {}
>
> Month::Month(Month const &copy) : numOfDays(copy.numOfDays) {}
>
> Month::~Month() {
> for (int num = 0; num < 12; num++)
> if (months[num]) delete months[num];

Add setting them to 0 too.

> }
>
> Month *Month::getMonth(int num) {
> if (! months[num]) {
> switch (num) {
> case 1:
> months[num] = new Month(29); break;
> case 8:
> case 5:
> case 3:
> case 10:
> months[num] = new Month(30); break;
> default:
> months[num] = new Month(31);
> }
> }
> return months[num];
> }
> ----------------------------------------------------

V



Relevant Pages

  • Re: Best Coding Practice
    ... I needed to add an array class member to an object. ... OO standpoint to make a FreeProduct class which extends the Product ... Cost is an attribute, and different values for attributes should not determine new products. ...
    (comp.lang.php)
  • Re: Best Coding Practice
    ... I needed to add an array class member to an object. ... the logic was the same between the two arrays aside from the price, ... and the child classes the specific elements. ...
    (comp.lang.php)
  • calloc problem
    ... I am trying to dynamically allocate memory to be used as an array. ... The array is pointed to by the 'vert' member of the structure 'grid'. ... I wish for this array to be a 2 dimensional array of the struct ...
    (comp.lang.c)
  • Re: Best Coding Practice
    ... I needed to add an array class member to an object. ... wouldn't feel entirely comfortable suddenly making it a child class of ... OO standpoint to make a FreeProduct class which extends the Product ...
    (comp.lang.php)
  • Re: Best Coding Practice
    ... I needed to add an array class member to an object. ... wouldn't feel entirely comfortable suddenly making it a child class of ... Product with a cost of 0. ...
    (comp.lang.php)

Loading