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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:08:15+00:00 2026-05-29T11:08:15+00:00

I am a java developer, but I have had to learn C++ recently and

  • 0

I am a java developer, but I have had to learn C++ recently and I am confused about some things. What I would like to do is create a ‘global’ header file, which has a list of #define variables which will be constant throughout the suite I am creating. I created the header file, and I added some variables

#ifndef CONSTANTS_H
#define CONSTANTS_H

#define SM_START            1001;
#define SM_PAUSE            1002;
#define SM_STOP             1003;
#define SM_SAVE             1004;
#define SM_DISCARD          1005;
#define SM_SETUP            1007;


#endif // CONSTANTS_H

My problem is that I can’t access these…

I have included the header file where I need it, but there is no way for me to access the constants inside of it. Do I Have to have a .cpp file? is there a way for me to access the constant variables?

  • 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-29T11:08:15+00:00Added an answer on May 29, 2026 at 11:08 am

    First: You shouldn’t put the semicolons at the end of the #define. #define is a preprocessor directive, meaning that it basically does text replacement of the defined name with the content. So if you do something like int a = SM_STOP + 1; it would be preprocessed to int a = 1003; + 1; with your code, which is not what you want.

    Second: Headers are generally not compiled themselves but only for inclusion into *.cpp files or other headers (where #include is once again a text substitution). Therefore, yes you need to have a .cpp file somewhere (well not exactly, first of all you can choose a different extension and second you could even give the compiler a header as compile unit, but I would advise against it, at least until you know what you are doing). However you do not need to have a .cpp file for your constants, just #include your header into whatever file you want to use the constants in.

    Third: Why are you using preprocessor defines here? This seems to be like a perfect job for an enum. Then you could put it into a namespace/struct for removing the need to prefix them (with SM_). Or you could just use C++11’s new enum class, which behaves much like java’s enums. I would avoid preprocessors wherever possible. Since it is simply text replacement and it doesn’t respect any scoping and such, which makes it easy to get into problems (like with your semicolons).

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

Sidebar

Related Questions

My background is primarily as a Java Developer, but lately I have been doing
I'm a Java developer and I have a question about automating a task I've
I'm a Java developer looking to learn some C#/ASP.NET. One thing I've never liked
I'm normally a Java developer, but I'm writing a C++ library right now they
I've been a java developer for a couple years and have heard that you
Being a Java developer in an agile development cycle, I have learnt that it
I am new to Java and referring Head-First book. Do I have to learn
I had recently stabilised developments of a major open source library written in Java.
I am a novice software developer and have only developed a few Java based
I actually happily design and develop Java EE Applications for quite 9 years, but

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.