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 6350585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:50:18+00:00 2026-05-24T21:50:18+00:00

I just wrote a class with some static data members, but now I am

  • 0

I just wrote a class with some static data members, but now I am getting errors about "undefined references". Why doesn’t this work? What am I doing wrong?

(Note: This is meant to be an entry to Stack Overflow’s C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.)

  • 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-24T21:50:18+00:00Added an answer on May 24, 2026 at 9:50 pm

    To understand this, you should have a good understanding of compiling and linking, and the differences between declarations and definitions.


    Consider the following class:

    //In header file
    class Example {
        static bool exampleStaticMember;
    };
    

    Here, exampleStaticMember is declared but not defined. This means that if exampleStaticMember is used in a way that means that it must have an address then there must be a separate definition for it. In general, no declaration of a static data member in a class definition is a definition of that member.

    The required declaration is usually put in the cpp file which contains the other definitions for the members of the class. It must be in the same namespace as the class definition. The definition typically looks like:

    //In source file:
    //This may optionally have an initialiser (eg "= true")
    bool Example::exampleStaticMember; 
    

    The definition can be put in any cpp file, but it should not be put in the header with the class, because that would be likely to break the One Definition Rule.

    As a special case, if the static member variable is an const integral or enumeration type then it can have an initialiser in the class definition:

    //In header file
    class Example {
        static const int initialised = 15;
    };
    

    In this case, the definition in the cpp file is still required, but it is not allowed to have an initialiser:

    //In source file
    //Note: no initialiser!
    const int Example::initialised;
    

    Static members that have been initialised like this can be used in constant expressions.

    Templates

    For a static data member of a template, things are slightly different. The static member should be defined in the header along with the rest of the class:

    //In header file
    template<typename T>
    class Example {
        static int exampleInt;
        static T exampleT;
    }
    template<typename T> int Example<T>::exampleInt;
    template<typename T> T Example<T>::exampleT;
    

    This works because there is a specific exception to the One Definition Rule for static data members of class templates.

    Other uses of static

    When the static keyword is applied to functions and objects that are not in a class scope it can take on a very different meaning.

    When applied to objects in a function scope, it declares an object that is initialised in the first execution of the function and that subsequently keeps its value between function calls.

    When applied to objects or functions at namespace scope (outside of any class or function definition), it declares objects or functions with internal linkage. This usage is deprecated for objects, as the unnamed-namespace provides a better alternative.

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

Sidebar

Related Questions

I just wrote some new utility methods within a non-page class for an existing
I just wrote a new web part and now I am getting this error
I just wrote some code to test the behavior of std::equal, and came away
I'm running some test to prove a concept and i just wrote this code
I just wrote a simple DirectShow Filter (which inherits from CTransformFilter). But I want
We have a certain database that has some employee information, and I just wrote
I just wrote my first web service so lets make the assumption that my
I just wrote the following C++ function to programmatically determine how much RAM a
I just wrote an if statement in the lines of if (value == value1
I just wrote a PHP login script, and what I'm trying to accomplish is

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.