LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 11-12-2005, 12:05 AM   #1
astorm
LQ Newbie
 
Registered: Nov 2005
Distribution: Slackware
Posts: 23

Rep: Reputation: 15
"undefined reference"linker error for static field w/ C++


I get the linker error

/tmp/ccHfwmmN.o(.text+0x12): In function `Test::Foo()':
: undefined reference to `Test::signList'

with
Code:
class Test {
public:
  static vector<string> bar;
  static void Foo();
}

void Test::Foo()
{
  bar.push_back("donut snack");
}

int main()
{
  Test::Foo();
}
I tried initializing bar to NULL, but then the compiler says that bar is undeclared. Any help at all would be greatly appreciated.

Thanks in advance,
Alek
 
Old 11-12-2005, 12:51 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You only "declared" bar. Somewhere, you need to define (allocate space for) the actual variable "Test::bar".

Last edited by paulsm4; 11-12-2005 at 12:59 AM.
 
Old 11-12-2005, 12:55 AM   #3
coldAndUgly
Member
 
Registered: Dec 2003
Location: New Zealand
Distribution: Slackware 10.2
Posts: 36

Rep: Reputation: 15
Hi, both static member functions and data members must be defined somewhere.
Add this definition after the class.
Code:
vector<string> Test::bar;
Only constant integral static members may be initialised.
Code:
class Test {
public:
        static const int i = 0;
};
 
Old 11-12-2005, 09:06 PM   #4
astorm
LQ Newbie
 
Registered: Nov 2005
Distribution: Slackware
Posts: 23

Original Poster
Rep: Reputation: 15
Thanks a lot for your help. It works now,
 
Old 11-12-2005, 09:14 PM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Cool! That particular subtlety bit me the first time I played with static members, too!
 
Old 08-27-2008, 03:00 AM   #6
jpmueller
LQ Newbie
 
Registered: Aug 2008
Posts: 1

Rep: Reputation: 0
Thumbs up Thanks!

I had that problem too, it works perfect now. Thank you!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
"gcc linker" does not link C++ programs ( gxx_personality undefined ?) bahramH Programming 9 12-25-2009 01:38 PM
Several "undefined reference to" error when compiling a C programm acer_peri Programming 8 12-11-2008 05:45 AM
Kernel make bzImage error "undefined reference" w/ Prism2.5 USB driver under 2.6 joachimvb Mandriva 1 09-24-2004 07:43 PM
G++ Linking Error "undefined reference" djjumper9 Programming 2 04-13-2004 09:36 AM
ncurses compile error: "undefined reference" Mr. Eek Linux From Scratch 1 05-12-2003 04:25 PM


All times are GMT -5. The time now is 03:33 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration