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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:27:44+00:00 2026-06-14T12:27:44+00:00

I am trying to define a class Util with a static member variable MAX_DIST,

  • 0

I am trying to define a class Util with a static member variable MAX_DIST, in the following sense,

class Util{

 public:
    static double MAX_DIST;
    Util():MAX_DIST(400.0){}
};

and be able to update it in some other class, e.g.

Util::MAX_DIST = 387.98;

This gives me an error:

‘double Util::MAX_DIST’ is a static data member; it can only be initialized at its definition

However, if I initialize MAX_DIST at its definition, such as

class Util{

 public:
    static const double MAX_DIST = 400;
    Util();
};

(I have to add the ‘const’ as instructed by the compiler, otherwise I will get an “ISO C++ forbids in-class initialization of non-const static member” error)
Now I can not modify MAX_DIST in other places since it is now ready only:

error: assignment of read-only variable ‘Util::MAX_DIST’

After fruitless search on the internet, I can not find a solution to this paradox. Can someone help me out?

  • 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-14T12:27:45+00:00Added an answer on June 14, 2026 at 12:27 pm

    However, if I initialize MAX_DIST at its definition

    You’re confusing definition and declaration. You are trying to initialise in the latter – C++ forbids that. The other answers have shown you how the definition looks like: it has to be outside the class declaration, and in its own compilation unit (otherwise you’ll violate the One Definition Rule when you try including the header in several source files).

    A word on why the original code didn’t work: you tried putting the initialisation into the initialiser list of the constructor. However, this constructor is called for every instance. Even if that code compiled, it would always reset your static variable – not what you want.

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

Sidebar

Related Questions

I'm trying to define base class, which contains typedef's only. template<typename T> class A
I am trying to define an abstract class that has operators to compare two
I'm stuck. I'm trying to dynamically define a class method and I can't wrap
I'm trying to use the util:constant tag for ioc, but I'm getting the following
I was trying to define a class like this: #include <library/foo.h> class my_class {
I am trying to create a class say MyStack that would implement a java.util.collections
i'm trying to define a User registration class and this is the function i
I'm trying to define a custom formatter for my class, but I can't find
I am trying to define a Java class which could be used for generic
So this a new one for me. I'm trying to define a ConfigurationSection class

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.