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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:07:35+00:00 2026-06-06T19:07:35+00:00

Possible Duplicate: GCC problem : using a member of a base class that depends

  • 0

Possible Duplicate:
GCC problem : using a member of a base class that depends on a template argument

I thought I was familiar with C++, but apparently not familiar enough.
The problem is when you define a constant in a template class, you can use the constant in new classes that derive from that class, but not new template classes that derive from it.

For example, gcc says

test.h:18: error: ‘theconstant’ was not declared in this scope

when I try to compile this (simplified) header file:

#pragma once

template <typename T> class base
{
  public:
    static const int theconstant = 42;
};

class derive1 : public base<size_t>
{
  public:
    derive1(int arg = theconstant) {}
};

template<typename T> class derive2 : public base<T>
{
  public:
    derive2(int arg = theconstant) {} // this is line 18
};

So the problem is that one class, derive1, compiles fine, but the other class, derive2, which is a template specialisation, does not.
Now maybe gcc’s error is not clear enough, but I don’t understand why the constructor in derive2 would have a different scope than the one in derive1.
In case it matters, this happens during compilation of the header file itself, not when instantiating an object of type derive2<type>.

I also know what to change to make this compile, so I’m not really looking for one-line pieces of code as answers. I want to understand why this happens! I tried searching the web, but apparently I’m not using the correct search arguments.

  • 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-06T19:07:36+00:00Added an answer on June 6, 2026 at 7:07 pm

    I am pretty sure this will help you understand:

    Your code which does not compile:

    template<typename T> class derive2 : public base<T>
    {
      public:
        derive2(int arg = theconstant) {} // this is line 18
    };
    

    And the reason why:

    template <> class base<size_t>
    {
      public:
        static const int ha_idonthave_theconstant = 42;
    };
    derive2<size_t> impossible_isnt_it; 
    

    The specialization!!! Compiler at your line 18 cannot be sure that you will not specialize the base<> in the way that this constant will not be present there at all.

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

Sidebar

Related Questions

Possible Duplicate: Printing derived class name in base class I am using GCC, and
Possible Duplicate: problem with template inheritance This code doesn't compile in GCC: template <typename
Possible Duplicate: Virtual Functions and Performance C++ Is this correct, that class member function
Possible Duplicate: trailing return type using decltype with a variadic template function I'm getting
Possible Duplicate: Get path of executable I'm programming on Windows using MinGW, gcc 4.4.3.
Possible Duplicate: trailing return type using decltype with a variadic template function I want
Possible Duplicate: gcc c++ virtual inheritance problem Hi All, I am reading Effective C++
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: How do you get assembler output from C/C++ source in gcc? Hello

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.