Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > C++ undefined symbols problem using a const static char array

C++ undefined symbols problem using a const static char array
Thread Tools
Fresh-Faced Recruit
Join Date: Jul 2001
Location: San Diego, CA
Status: Offline
Reply With Quote
Mar 24, 2002, 02:26 AM
 
I am having a problem using a const static char array as a class data member. Below is a simplification of my problem. The variable sStatic can be declared and initialized, but simply can't be accessed.

Thanks for any help,

Harvo


The compile command:

%> c++ t.cpp


The compiler response:

/usr/bin/ld: Undefined symbols:
__4Test.sStatic


The source file (each of the cerr statements alone produces the above error):

#include <iostream>

class Test {

public:
const static char sStatic[][15] = { "hello", "goodbye" };

Test() {

cerr << Test::sStatic[0] << endl;
cerr << sStatic[0] << endl;

}
};

int main() {
Test t;

cerr << t.sStatic[0] << endl;
cerr << Test::sStatic[0] << endl;

return 0;
}
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Mar 24, 2002, 05:16 AM
 
It's been a while since I've played with C++, but IIRC, you need to initialize your static const outside of the class definition so it's got file scope. Try this...

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#include &lt;iostream&gt;

class Test {

public:
const static char sStatic[][<font color = blue>15</font>];

Test() {

cerr &lt;&lt; Test::sStatic[<font color = blue>0</font>] &lt;&lt; endl;
cerr &lt;&lt; sStatic[<font color = blue>0</font>] &lt;&lt; endl;

}
};

Test::sStatic = { <font color = red>"hello"</font>, <font color = red>"goodbye"</font> };

</font>[/code]
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
reyzell  (op)
Fresh-Faced Recruit
Join Date: Jul 2001
Location: San Diego, CA
Status: Offline
Reply With Quote
Mar 24, 2002, 08:13 PM
 
I tried you're idea, and you're close. Below is what actually worked. The initialization of the variable requires the same declaration without the "static" keyword.

Thanks

#include &lt;iostream&gt;

class Test {

public:
const static char sStatic[][15];

Test() {

cerr &lt;&lt; Test::sStatic[0] &lt;&lt; endl;
cerr &lt;&lt; sStatic[0] &lt;&lt; endl;

}
};

const char Test::sStatic[][15] = { "hello", "goodbye" };

int main() {
Test t;

cerr &lt;&lt; t.sStatic[0] &lt;&lt; endl;
cerr &lt;&lt; Test::sStatic[0] &lt;&lt; endl;

return 0;
}
     
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status: Offline
Reply With Quote
Mar 24, 2002, 11:19 PM
 
In addition, I don't believe class member variables can be initialized inside the class definition. You have to create a constructor to initialize the variables. (of course, if they're public, you can initialize them however you want.)

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:25 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2