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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:15:28+00:00 2026-05-16T06:15:28+00:00

If I have the following in a header file: Foo.h Foo { public: static

  • 0

If I have the following in a header file:

Foo.h

Foo
{
public:
  static const int BAR = 1234;
  ...
};

Do I also need to define the variable in the .cpp, e.g.:

Foo.cpp

const int Foo::BAR;

We have an issue where initializing a static in a header seems to work on MS compilers but with gcc on the Mac it seems to give linker errors.

  • 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-16T06:15:29+00:00Added an answer on May 16, 2026 at 6:15 am

    You need both the declaration and the definition, just as you’ve written them.

    Since it is an integer, you can initialise it in the declaration as you’ve done, and the compiler should treat it as a compile-time constant when it can. But it still needs one (and only one) definition in a source file, or you’ll get link errors when it can’t be treated as a constant.

    Apparently, Microsoft decided that the standard behaviour was too confusing, and “extended” the language to treat a declaration with an initialiser as a definition; see this issue. The result is that you get link errors (multiply defined symbols) if you also define the symbol correctly. You can get the standard behaviour by disabling language extensions (/Za).

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

Sidebar

Related Questions

Consider the following header file example: shared_example.h #ifndef SHARED_EX #define SHARED_EX const int Shared_Int
in header file I have defined the following function #ifndef OS_H #define OS_H #include
Suppose you have the following file: textfield,datetimefield,numfield foo,2008-07-01 17:50:55.004688,1 bar,2008-07-02 17:50:55.004688,2 The Ruby code
I have the following code in my header file: class Factovisors { public: Factovisors(std::istream&
I have the following static array in header file: static MyStruct_t MyStructArray[] = {
I have following macro for measuring time in header file: #define TIMER_START(x) double ___timer__##x
So I have a base class with the following header file: #ifndef ODESolver_H #define
I have the following files. foo.h (C++ header file) foo.mm (C++ file) test_viewcontroller.h (objective
I have the following C Header/Code Example: Header file struct category_info { int id;
I have the following header file: #ifndef DATABASE_H #define DATABASE_H #include <vector> #include <iostream>

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.