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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:38:38+00:00 2026-06-04T09:38:38+00:00

If I work with classes, everything is just fine: struct Base1 { int value;

  • 0

If I work with classes, everything is just fine:

struct Base1 {
  int value;
  Base1(int value) : value(value) { }
};

struct Test1 : public Base1 {
  int getValue() { return value; }  
  Test1(int value) : Base1(value) { }
};

but with templates scope resolution is needed:

template <typename T>
struct Base {
  T value;
  Base(T value) : value(value) { }
};

template <typename T>
struct Test : public Base<T> {
  typedef Base<T> parent;
  T getValue() { return parent::value; }  // why do I need to use parent:: here?
  Test(T value) : parent(value) { }
};

Without the scope resolution I receive error 'value' was not declared in this scope (gcc compiler used).
Why?

  • 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-04T09:38:39+00:00Added an answer on June 4, 2026 at 9:38 am

    Because the compiler is not aware that value is dependent on the template argument. As such, it attempts to resolve it during the first pass (prior to instantiating the template), and fails.

    The two options are to use the scoping resolution, as you have, or use this->value. Since this is always a dependent name, this will force the evaluation to occur during the second pass.

    See http://ideone.com/07odY

    Edit: And as to the why it needs to be done at all:

    While Test<T> derives from Base<T>, due to template specialization you could make Base<std::string> (for example) be totally different than the normal Base<T>, and not have a member named value, or it could be of a different type, or anything. By forcing it to be a dependent name, the compiler is forced to wait until it knows the actual type involved before checking.

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

Sidebar

Related Questions

I understand how transactions work and everything functions as expected, but I do not
Is it bad policy to have lots of work classes(such as Strategy classes), that
Say I have two classes created work and workItem. CWorker *work = new CWorker();
I have a script that parse the database and create php classes to work
I work in the maintenance team of a big project (around 7k+ classes) and
I work with Entity Framework - add records in the database using classes that
I have several classes, and want to work with their collections like with a
I have a simple Poco-Model using abstract classes, and it seems not to work
Work on asp.net vs05. I have three type of value Like:IsDesign,IsPrinting,IsInstall they are bit
I am making some abstract base classes. The base object is defined as: public

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.