473,769 Members | 7,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(in t num) : numOfDays(num) {}

Month::Month(Mo nth const &copy) : numOfDays(copy. numOfDays) {}

Month::~Month() {
for (int num = 0; num < 12; num++)
if (months[num]) delete months[num];
}

Month *Month::getMont h(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 2046
"Neno" <m_**********@t iscali.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(in t num) : numOfDays(num) {}

Month::Month(Mo nth 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::getMont h(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.********@com Acast.net> ha scritto nel messaggio
news:CUied.1730 88$He1.158957@a ttbi_s01...
"Neno" <m_**********@t iscali.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(in t num) : numOfDays(num) {}

Month::Month(Mo nth 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::getMont h(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
12143
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. // test_const.h #ifndef HEADER_TEST #define HEADER_TEST
20
3154
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 extern linkage is inlined? Should the compiler still export the function? Or is an inlined function implicitly static?
10
6196
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: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
4
24435
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: warning: unused variable `foo'
6
2154
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, criticism, advices, improvements. Abbreviation: Created (C)
3
6036
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 inline may have external linkage while in others it has internal (from what I have read in comments by people). Are there any (other) places where linkage is ambiguous?
13
2096
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 object or a function contains the storage-class specifier extern , the identifier has the same linkage as any visible declaration of the identifier with file scope. If there is no visible declaration with file scope, the identifier has external...
2
3776
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 common meanings: static as in "statically allocated" as opposed to on the stack or on the free store and static as in "with restricted visibility" as opposed to with external linkage. For member functions, static has the second meaning."
12
5411
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 variable, but according to the standard there is a difference: "While this is essentially true in practical effect, there are subtle differences. Using static as shown here causes "i" to have internal
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.