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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:22:56+00:00 2026-05-25T17:22:56+00:00

How do I declare static constant values in C++? I want to be able

  • 0

How do I declare static constant values in C++?
I want to be able to get the constant Vector3::Xaxis, but I should not be able to change it.

I’ve seen the following code in another class:

const MyClass MyClass::Constant(1.0);

I tried to implement that in my class:

static const Vector3 Xaxis(1.0, 0.0, 0.0);

However I get the error

math3d.cpp:15: error: expected identifier before numeric constant
math3d.cpp:15: error: expected ‘,’ or ‘...’ before numeric constant

Then I tried something more similar to what I’d do in C#:

static Vector3 Xaxis = Vector3(1, 0, 0);

However I get other errors:

math3d.cpp:15: error: invalid use of incomplete type ‘class Vector3’
math3d.cpp:9: error: forward declaration of ‘class Vector3’
math3d.cpp:15: error: invalid in-class initialization of static data member of non-integral type ‘const Vector3’

My important parts of my class so far look like this

class Vector3
{
public:
    double X;
    double Y;
    double Z;

    static Vector3 Xaxis = Vector3(1, 0, 0);

    Vector3(double x, double y, double z)
    {
        X = x; Y = y; Z = z;
    }
};

How do I achieve what I’m trying to do here? To have a Vector3::Xaxis which returns Vector3(1.0, 0.0, 0.0);

  • 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-25T17:22:57+00:00Added an answer on May 25, 2026 at 5:22 pm
    class Vector3
    {
    public:
        double X;
        double Y;
        double Z;
    
        static Vector3 const Xaxis;
    
        Vector3(double x, double y, double z)
        {
            X = x; Y = y; Z = z;
        }
    };
    
    Vector3 const Vector3::Xaxis(1, 0, 0);
    

    Note that last line is the definition and should be put in an implementation file
    (e.g. [.cpp] or [.cc]).

    If you need this for a header-only module then there is a template-based trick that
    do it for you – but better ask separately about that if you need it.

    Cheers & hth.,

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

Sidebar

Related Questions

If I have a collection of static constants that I want to declare centrally
I want to forward declare a static member function of a class in another
I want to use CodeDOM to both declare and initialize my static field in
Not able to access static fields with OGNL with struts2 <s:checkbox name=operation fieldValue=@com.xx.xxx.webapp.action.EntryAction@OPERATIONAL />
I want a static constant, LIST_DELIMITER , defined in my class below. However, I
I declare a static char array, then I pass it to a function. How
How to declare a static dictionary object inside a static class? I tried public
When trying to declare a static array in my program I receive a static
I am thinking it is a best practice to declare them as static, as
How do you declare an Objective-C static method that can be called without a

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.