linkage error when initializing static member array

Discussion in 'C++' started by Neno, Oct 23, 2004.

  1. Neno

    Neno Guest

    Hi,
    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?
    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"

    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];
    }

    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];
    }
    ----------------------------------------------------

    Thank you to all the people in the newsgroup,
    Neno.
    Neno, Oct 23, 2004
    #1
    1. Advertising

  2. "Neno" <> 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
    Victor Bazarov, Oct 23, 2004
    #2
    1. Advertising

  3. Neno

    Neno Guest

    Thank you !
    It works now. :)
    Bye.

    "Victor Bazarov" <> ha scritto nel messaggio
    news:CUied.173088$He1.158957@attbi_s01...
    > "Neno" <> 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
    >
    >
    Neno, Oct 23, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    1
    Views:
    6,616
    Siemel Naran
    Apr 19, 2004
  2. Bill Pursell

    simulating private member functions--static linkage

    Bill Pursell, Apr 27, 2006, in forum: C Programming
    Replies:
    4
    Views:
    326
    Michael Wojcik
    May 1, 2006
  3. tropos
    Replies:
    3
    Views:
    447
  4. dolphin
    Replies:
    3
    Views:
    1,323
    Pete Becker
    Dec 5, 2007
  5. Replies:
    1
    Views:
    578
    Michael DOUBEZ
    Sep 12, 2008
Loading...

Share This Page