Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 778955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:51:01+00:00 2026-05-14T19:51:01+00:00

I want to have a simple class i can call to get a unique

  • 0

I want to have a simple class i can call to get a unique number whilst the program is running – i can do the below with a dynamic allocation, and then just delete when not needed, but i still wanted to get a static version too.
Strangely, the code below (which is seemingly straightforward) throws some strange comiple errors (appended below).

Any ideas whats going on ? is this an incorrect use of static ?

class Id_gen {
    private:
 //adding static here stops the code from compiling:
 static int curr_id;

    public:

 Id_gen() {curr_id = 1; cout<<"debug:constructed"; }
 int get_id() {curr_id++; return curr_id; };
};

int main () {


    Id_gen bGen;
    cout << bGen.get_id() <<endl;

return 0;    
}

running g++ (linux 64):

c++2.cpp:(.text._ZN6Id_genC1Ev[Id_gen::Id_gen()]+0xe): undefined reference to `Id_gen::curr_id'
/tmp/cc766N6p.o: In function `Id_gen::get_id()':
c++2.cpp:(.text._ZN6Id_gen6get_idEv[Id_gen::get_id()]+0xa): undefined reference to `Id_gen::curr_id'
c++2.cpp:(.text._ZN6Id_gen6get_idEv[Id_gen::get_id()]+0x13): undefined reference to `Id_gen::curr_id'
c++2.cpp:(.text._ZN6Id_gen6get_idEv[Id_gen::get_id()]+0x19): undefined reference to `Id_gen::curr_id'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:51:02+00:00Added an answer on May 14, 2026 at 7:51 pm

    Add the initialization/definition of the static member as:

    int Id_gen::curr_id = 0;
    

    after the class definition.

    EDIT: As mentioned in the comment by @sbi : Initialization is optional, the linker requires the definition only.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple class that I want to save to SQL Server
I have a simple dropdown and want to get the specific value of an
Windows 7 Firefox 10.0.1 I have simple setTimeout logic that I want to test
I have a simple form on a page and I want to auto submit
i have a simple chat application in flex 3 and i want to make
I have a simple WCF Data Services service and I want to expose a
I have a simple jsp/servlet application and I want to add AJAX feature to
I have a simple xml file and I want to remove everything before the
I have a simple quiz application and I want display a nice timer /
I have a simple animation the problem is I want the animation to end

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.