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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:22:25+00:00 2026-06-06T16:22:25+00:00

here is what I need to do : #include <iostream> using namespace std; class

  • 0

here is what I need to do :

#include <iostream>                                                                                                                                                                                              

using namespace std;

class A
{
    public :
    virtual void fa() = 0;
};

template <typename type>
class B : public A
{
    protected :
    int v_b;
};

template <typename type>
class C : public B<type>
{
    public :
    void fa()
    {
        // whatever action that try to access v_b
        cout << "v_b = " << v_b << endl;
    }
};

int main()
{
    C<int> toto;
    toto.fa();
    return 0;
}

and here is g++ output :

test.cpp: In member function ‘void C<type>::fa()’:
test.cpp:25:29: error: ‘v_b’ was not declared in this scope

In my understanding, v_b is a protected member of B and is therefore accessible in C. A and B are both abstract classes, and I need to override the f_a() method of A in class C in order to instanciate it. Since the compiler is telling me this

test.cpp: In member function ‘void C<type>::fa()’:

and NOT

‘void A::fa()’:


i don’t get why the v_b variable is not present in the scope. Is this a problem in the way I use templates?

Can anyone help me out on that one ?

thx

edit :

I tryed to use this->v_b or B<type>::v_b as suggested here and it worked fine ! thx for your help

  • 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-06T16:22:26+00:00Added an answer on June 6, 2026 at 4:22 pm

    In the expression:

        cout << "v_b = " << v_b << endl;
    

    v_b is a non-dependent expression (i.e. it does not look like it depends on the template arguments. For a non-dependent expression the first phase lookup must resolve the symbol, and it will do so by looking only in non-dependent contexts. This does not include a templated base (as it does depend on the type argument). The simple fix is to qualify the call with this:

        cout << "v_b = " << this->v_b << endl;
    

    Now it is a dependent expression (this which clearly depends on the instantiating type), and lookup is delayed to the second phase where the type is substituted and the bases can be checked.

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

Sidebar

Related Questions

Here the code #include <iostream> #include <conio.h> using namespace std; template <typename T> class
here is implementation IntSetList in c++ #include <iostream> using namespace std; class IntSetList{ private:
#include<iostream> using namespace std; class Something { public: int j; Something():j(20) {cout<<Something initialized. j=<<j<<endl;}
here is code #include <iostream> #include <fstream> #include <cstring> using namespace std; int main()
#include <iostream> using namespace std; void generCad(int n, char* cad){ int longi = 1,
I'm very surprised to find that the following compiles: #include <iostream> using namespace std;
With #include <iostream> using namespace std; int a = 1; int main() { int
I have: car.cc #include car.h #include <iostream> using namespace std; extern C Car* create_object()
#include <iostream> using namespace std; /* TA <-- defines static function / \ |
I've got an error while using find() function. Here is the code: #include <iostream>

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.