LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-07-2011, 02:20 PM   #1
l_long_island
LQ Newbie
 
Registered: Feb 2008
Posts: 25

Rep: Reputation: 1
Question C++; Problems initializing static member array


Can anyone shed some light on the the following issue? The code below defines an array of structures in a class. I would like this to be a “Static” array and then initialize the array in a initialization method.
I understand that initialization lists are the way to go but due to the size and complexity of the array I plan on using (the example below is a simple snippet that illustrates the problem) I would like to use the initialization method. However, I can’t even get this thing to compile. The error says the reference to the ArrayOfStructs in the initialization routine is undefined. Any understanding of this issue would be greatly appreciated.
l_long_Island
P.S. This is the error message
undefined reference to `ClassWithStaticArray::ArrayOfStruts'

class ClassWithStaticArray {
public:

struct IntegerStruct
{
int Item1;
int Item2;
};
static IntegerStruct ArrayOfStructs [2];


static void InitArray ()
{
ArrayOfStructs [0].Item1 = 0; // Error messages say
ArrayOfStructs [0].Item2 = 0; // ArrayOfStructs is undefined
ArrayOfStructs [1].Item1 = 0;
ArrayOfStructs [1].Item2 = 0;

};

ClassWithStaticArray();
virtual ~ClassWithStaticArray();

};

Last edited by l_long_island; 06-07-2011 at 05:04 PM.
 
Old 06-07-2011, 02:30 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
If anything, the error is here:
Code:
ArrayOfStruts [1].Item1 = 0;
ArrayOfStruts [1].Item2 = 0;
Perhaps you mean to declare an array of two IntegerStruct objects?
Code:
static IntegerStruct ArrayOfStruts [2];
You are probably missing the following statement before your main() function:
Code:
ClassWithStaticArray::IntegerStruct ClassWithStaticArray::ArrayOfStruts [2];

Last edited by dwhitney67; 06-07-2011 at 02:34 PM.
 
Old 06-07-2011, 02:32 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by l_long_island View Post
undefined reference to `ClassWithStaticArray::ArrayOfStruts'
That isn't the error message I would have expected for this problem. But it seems to fit.

Code:
class ClassWithStaticArray {
...
    static IntegerStruct ArrayOfStruts [1];
...
};
That code only declares ArrayOfStruts. It doesn't define it.

Somewhere outside the class definition (and subject to the One Definition Rule) you need:

Code:
ClassWithStaticArray::IntegerStruct ClassWithStaticArray::ArrayOfStruts [1];
Edit: I didn't see dwhitney67's reply before posting. His comment about using two elements of a 1 element array is also correct.

Last edited by johnsfine; 06-07-2011 at 02:37 PM.
 
Old 06-07-2011, 03:41 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Is it a typo or is it supposed to be "ArrayOfStruts" instead of "ArrayOfStructs"?
 
Old 06-07-2011, 05:11 PM   #5
l_long_island
LQ Newbie
 
Registered: Feb 2008
Posts: 25

Original Poster
Rep: Reputation: 1
Responders,
Yes, that should have been a [2] (vs. [1]) and yes that should have been "struct" (vs. "strut"). I put the line you suggested outside the class, in the corresponding .cpp file and ureka it worked !!
I will now try it in the software project that contains the non-trivial data structures. I will let you know how it goes.
Thanks.
l_long_island

Last edited by l_long_island; 06-07-2011 at 05:30 PM. Reason: grammar
 
Old 06-07-2011, 05:32 PM   #6
l_long_island
LQ Newbie
 
Registered: Feb 2008
Posts: 25

Original Poster
Rep: Reputation: 1
Gentlemen (if I may assume that),

I implemented your suggestion in the non-trival code and it is compiling (finally) Thanks much !!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
initializing Clocksource with static __init and not as a module bizoo Programming 0 12-02-2010 02:19 PM
Initializing drive array command for class lab on fedora 12 stefnthebox Linux - Newbie 4 02-12-2010 06:36 AM
How to initialize a static array of a class in a static member function lali.p Programming 9 02-16-2008 09:27 AM
Initializing new array tbone63078 Red Hat 1 07-25-2007 02:00 PM
Initializing Array of Strings jrtayloriv Programming 10 02-03-2005 12:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration