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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:33:18+00:00 2026-06-01T23:33:18+00:00

The code below works as long as I keep it all in the main.cpp

  • 0

The code below works as long as I keep it all in the “main.cpp” file.

//#include "Travel.h"
//#include "Obj.h"

// "Travel.h"
typedef int travel_t;

class Travel
{

public:

    static const travel_t AIR;
    static const travel_t WATER;
    static const travel_t LAND;

};

// "Travel.cpp"
// #ifndef TRAVEL_H
// #define TRAVEL_H
//
// #include "Travel.h"
const travel_t Travel::AIR = -2;
const travel_t Travel::WATER = -1;
const travel_t Travel::LAND = 0;
// #endif //TRAVEL_H

// "Obj.h"
// #ifndef OBJ_H
// #define OBJ_H
//
//#include "Travel.h"
template<typename T, travel_t travel>
class Obj
{
public:
    void foo(){};
};
// #endif //OBJ_H

// "main.cpp"
int main()
{
    Obj<int, Travel::AIR> objAirTravel;

    objAirTravel.foo();

    return 0;
}

However, as soon as I moved code to different headers and implementation files as indicated, it doesn’t compile any more. 🙁 How can I fix that problem? What is the problem/rule behind it? This is the compiler error I get (using gcc):

main.cpp|45|error: 'Travel::AIR' is not a valid template argument for type 'int' because it is a non-constant expression|
main.cpp|45|error: invalid type in declaration before ';' token|
main.cpp|47|error: request for member 'foo' in 'objAirTravel', which is of non-class type 'int'|
  • 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-01T23:33:19+00:00Added an answer on June 1, 2026 at 11:33 pm

    In order to use a constant as a template argument, its value must be available in the current translation unit. When you move the definition of Travel::Air to a different source file, its value is no longer available to the compiler in main.

    Since it’s an integer constant, you can declare the value in the declaration inside the class:

    class Travel
    {
    public:
        static const travel_t AIR = -2;
        static const travel_t WATER = -1;
        static const travel_t LAND = 0;
    };
    

    Now the values are available to use as template arguments in any translation unit that includes this class definition.

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

Sidebar

Related Questions

The code below works great except the email has all the text on one
This code below works in all web browsers except IE: <input type="text" name="passwordLogin" value="Password"
I'm using the code below within a loop. It works so long as I'm
The code below works as long as the argument in the run configuration equals
The code below works. But if I comment out the line Dim objRequest As
the code below works fine but it takes an absolute age to run. How
The code below works great. I have a MySQL database that contains book titles
The code below works nicely. Each title in the MySQL table submission is printed
The code below works unless p.School.SchoolName turns out to be null, in which case
The code below works in FF, Safari, Chrome. But IE is giving me issues.

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.