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

  • Home
  • SEARCH
  • 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 6255529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:22:54+00:00 2026-05-24T14:22:54+00:00

I was reading intro on gtest and found this part confusing: The compiler complains

  • 0

I was reading intro on gtest and found this part confusing:

The compiler complains about “undefined references” to some static
const member variables, but I did define them in the class body.
What’s wrong?

If your class has a static data member:

// foo.h
class Foo {
  ...
  static const int kBar = 100;
};

You also need to define it outside of the class body in foo.cc:

const int Foo::kBar;  // No initializer here.

Otherwise your code is invalid C++, and may break in unexpected
ways. In particular, using it in Google Test comparison assertions
(EXPECT_EQ, etc) will generate an “undefined reference” linker error.

Can somebody explain why defining a static const in in a class without defining it outside class body is illegal C++?

  • 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-24T14:22:56+00:00Added an answer on May 24, 2026 at 2:22 pm

    First things first, inside a class body is not a definition, it’s a declaration. The declaration specifies the type and value of the constant, the definition reserves storage space. You might not need the storage space, for instance if you only use the value as a compile time constant. In this case your code is perfectly legal C++. But if you do something like pass the constant by reference, or make a pointer point to the constant then you are going to need the storage as well. In these cases you would get an ‘undefined reference’ error.

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

Sidebar

Related Questions

I just finished reading about scoping in the R intro , and am very
Okay I am officially confused. I'm reading up on this MVC intro in php
I was just reading the http://www.meebo.com/ About Us page, and read this line :
Hoping for some help after reading into MVC routing and not coming up with
I am reading Beej's Guide to network programming . In one of his intro
I am doing some reading into the benefits of using the NoSQL style of
I found this snippet of code here that allows you to log into a
I'm a little confused about how reading data into a stream works in asio.
I started reading emacs lisp intro. I haven't seen any mentioning of data types.
I am reading Apple's sample codes at: http://developer.apple.com/library/mac/#samplecode/CocoaSlides/Introduction/Intro.html it works on a NSView #import

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.