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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:05:52+00:00 2026-06-14T09:05:52+00:00

Possible Duplicate: C++ – defining static const integer members in class definition Note: There

  • 0

Possible Duplicate:
C++ – defining static const integer members in class definition

Note: There are several extant questions re similar issues, but I have reviewed many of them and cannot find an answer that explains this behavior:

Say I have code such as the following (in a header file)

class Foo {
    static const int TEST = 33;

    public:
    void willItWork(void) {
        printf("%d", std::max(TEST, 75));  // embedded platform, no streams
    }
};

int main(void) {
    Foo tester;
    tester.willItWork();
}

This will compile, but it will not link. I get the linker error

Error: L6218E: Undefined symbol Foo::TEST (referred from foo.o).

It only seems like passing the value to outside functions causes problems. Using TEST in ordinary expressions or functions within the class works fine. If I instead write willItWork() as

void willItWork(void) {
    int diff = TEST - 23;
    printf("%d", diff);
}

there is no error.

I found another question that referenced the C++ standard regarding this (Section 9.4.2):

If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a const-initializer which shall be an integral constant expression.

Since what I have done seems to be “within the rules,” can anyone think of any possible explanation for this odd behavior?

I tried similar code on ideone and had no issue (however, I can not mimic the exact structure, i.e. with header files, there). Does this mean the linker I am using doesn’t quite conform to the standard here?

Any insight is greatly appreciated. I can always provide more information, too.

  • 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-06-14T09:05:55+00:00Added an answer on June 14, 2026 at 9:05 am

    If the compiler feels it needs the address of the static member variable, e.g., when binding the variable to a reference at some point, it will create a corresponding undefined symbol and you will have to define the member:

    int const foo::TEST;
    

    (in one translation unit). If the compiler only ever accesses a the value you can get away with not defining the object. Unless you need the type to be an int, you can use an enum instead and avoid the need for defining the member:

    enum { TEST = 33 };
    

    The term in the standard to look for is odr-used if I recall correctly.

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

Sidebar

Related Questions

Possible Duplicate: Objective C for Windows iPhone development on Windows Is there any way
Possible Duplicate: Circular definition in C typedef struct{ node *next; node *last; } doubleLink;
Possible Duplicate: Why should the implementation and the declaration of a template class be
Possible Duplicate: Can a Bash script tell what directory it's stored in? Is there
Possible Duplicate: Matching an integer between the brackets What is the regex for matching
Possible Duplicate: Create Generic method constraining T to an Enum Is there any reason
Possible Duplicate: How to check if jQuery object exist in array? is there any
Possible Duplicate: Pure virtual destructor in C++ class A{ public: virtual ~A()=0; }; class
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web

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.