472,971 Members | 2,075 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,971 software developers and data experts.

linkage error when initializing static member array

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.
Jul 22 '05 #1
2 2019
"Neno" <m_**********@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
Jul 22 '05 #2
Thank you !
It works now. :-)
Bye.

"Victor Bazarov" <v.********@comAcast.net> ha scritto nel messaggio
news:CUied.173088$He1.158957@attbi_s01...
"Neno" <m_**********@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

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. //...
20
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with...
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
4
by: Mantorok Redgormor | last post by:
Is this legal? int foo = { 0 }; gcc gives: foo.c: In function `main': foo.c:5: warning: missing braces around initializer foo.c:5: warning: (near initialization for `foo') foo.c:5:...
6
by: Neelesh Bodas | last post by:
Hello All, I was just listing down various ways in which variables can be created and destroyed in C++. (On the lines of 10.4.3 TC++PL Ed 3) Putting the summary here for corrections, comments,...
3
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places...
13
by: fctk | last post by:
source: http://rm-f.net/~orange/devel/specifications/c89-draft.html#3.1.2.2 there are two passages in this paragraph i can't fully understand: 1) "If the declaration of an identifier for an...
2
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the...
12
by: Taras_96 | last post by:
Hi everyone, AFAIK external linkage allows you to refer to variables/functions outside of the current translation unit. A variable in an unnamed namespace is similar to declaring a static...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.