Class inaccessible after inheritance












3















If you compile the code below it fails, saying that class B is inaccessible where it is used as an argument to the member function func. Why is this?



Note: if D2 does not inherit from D1 then the error goes away, so somehow the inheritance from D2 makes B inaccessible.



namespace myns {
class B {};
}

using namespace myns;

class D1 : B {};

class D2 : D1 {
void func(B b) {}
};









share|improve this question

























  • You did a fantastic job of obfuscating your own question by the simple omission of the actual compile error. stackoverflow.com/questions/how-to-ask

    – kfsone
    Sep 5 '13 at 5:25
















3















If you compile the code below it fails, saying that class B is inaccessible where it is used as an argument to the member function func. Why is this?



Note: if D2 does not inherit from D1 then the error goes away, so somehow the inheritance from D2 makes B inaccessible.



namespace myns {
class B {};
}

using namespace myns;

class D1 : B {};

class D2 : D1 {
void func(B b) {}
};









share|improve this question

























  • You did a fantastic job of obfuscating your own question by the simple omission of the actual compile error. stackoverflow.com/questions/how-to-ask

    – kfsone
    Sep 5 '13 at 5:25














3












3








3


1






If you compile the code below it fails, saying that class B is inaccessible where it is used as an argument to the member function func. Why is this?



Note: if D2 does not inherit from D1 then the error goes away, so somehow the inheritance from D2 makes B inaccessible.



namespace myns {
class B {};
}

using namespace myns;

class D1 : B {};

class D2 : D1 {
void func(B b) {}
};









share|improve this question
















If you compile the code below it fails, saying that class B is inaccessible where it is used as an argument to the member function func. Why is this?



Note: if D2 does not inherit from D1 then the error goes away, so somehow the inheritance from D2 makes B inaccessible.



namespace myns {
class B {};
}

using namespace myns;

class D1 : B {};

class D2 : D1 {
void func(B b) {}
};






c++ namespaces






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 17:16









Baum mit Augen

41k12118152




41k12118152










asked Sep 5 '13 at 3:21









MatMat

8719




8719













  • You did a fantastic job of obfuscating your own question by the simple omission of the actual compile error. stackoverflow.com/questions/how-to-ask

    – kfsone
    Sep 5 '13 at 5:25



















  • You did a fantastic job of obfuscating your own question by the simple omission of the actual compile error. stackoverflow.com/questions/how-to-ask

    – kfsone
    Sep 5 '13 at 5:25

















You did a fantastic job of obfuscating your own question by the simple omission of the actual compile error. stackoverflow.com/questions/how-to-ask

– kfsone
Sep 5 '13 at 5:25





You did a fantastic job of obfuscating your own question by the simple omission of the actual compile error. stackoverflow.com/questions/how-to-ask

– kfsone
Sep 5 '13 at 5:25












3 Answers
3






active

oldest

votes


















5














Name lookup finds D2::D1::B, not myns::B. After name lookup, access check is performed, and discovers that D2::D1::B is private.



The namespace is a red herring: the exact same outcome is observed if B is defined in the global namespace.






share|improve this answer































    4














    You need to add the :: operator. Lookup is finding the injected-class-name instead of the myns::B.



    class D2 : D1 {
    void func(::B b) {}
    };


    11.1p5




    5 [ Note: In a derived class, the lookup of a base class name will
    find the injected-class-name instead of the name of the base class in
    the scope in which it was declared. The injected-class-name might be
    less accessible than the name of the base class in the scope in which
    it was declared. —end note ]
    [Example:




    class A { };
    class B : private A { };
    class C : public B {
    A *p; // error: injected-class-name A is inaccessible
    ::A *q; // OK
    };





    share|improve this answer





















    • 1





      I'm very interesting why we need class name injection? What the problem it want to solve, any code example?

      – ZijingWu
      Sep 5 '13 at 8:06








    • 1





      @ZijingWu: Here is one example, would you rather write function f1 or f2? The injected-class-name makes A f2() {return A();} possible.

      – Jesse Good
      Sep 5 '13 at 9:52











    • so injected-class-name is try to make reference class template easier? And even in this case why the base class name also be injected into the scope, which cause a lot of confusing like this post, I see the same question more than once in SO.

      – ZijingWu
      Sep 5 '13 at 10:04



















    0














    The default access specifier when inheriting a class/struct is "private", so your problem is this:



    class D1 : B {};
    class D2 : D1 {};


    Add the keyword "public" and voila.



    http://ideone.com/T5RpUM



    namespace myns {
    class B {};
    }

    using namespace myns;

    class D1 : public B {};

    class D2 : public D1 {
    void func(B b) {}
    };

    int main() {
    D2 d();
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18627191%2fclass-inaccessible-after-inheritance%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      Name lookup finds D2::D1::B, not myns::B. After name lookup, access check is performed, and discovers that D2::D1::B is private.



      The namespace is a red herring: the exact same outcome is observed if B is defined in the global namespace.






      share|improve this answer




























        5














        Name lookup finds D2::D1::B, not myns::B. After name lookup, access check is performed, and discovers that D2::D1::B is private.



        The namespace is a red herring: the exact same outcome is observed if B is defined in the global namespace.






        share|improve this answer


























          5












          5








          5







          Name lookup finds D2::D1::B, not myns::B. After name lookup, access check is performed, and discovers that D2::D1::B is private.



          The namespace is a red herring: the exact same outcome is observed if B is defined in the global namespace.






          share|improve this answer













          Name lookup finds D2::D1::B, not myns::B. After name lookup, access check is performed, and discovers that D2::D1::B is private.



          The namespace is a red herring: the exact same outcome is observed if B is defined in the global namespace.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 5 '13 at 3:31









          Igor TandetnikIgor Tandetnik

          32.3k33456




          32.3k33456

























              4














              You need to add the :: operator. Lookup is finding the injected-class-name instead of the myns::B.



              class D2 : D1 {
              void func(::B b) {}
              };


              11.1p5




              5 [ Note: In a derived class, the lookup of a base class name will
              find the injected-class-name instead of the name of the base class in
              the scope in which it was declared. The injected-class-name might be
              less accessible than the name of the base class in the scope in which
              it was declared. —end note ]
              [Example:




              class A { };
              class B : private A { };
              class C : public B {
              A *p; // error: injected-class-name A is inaccessible
              ::A *q; // OK
              };





              share|improve this answer





















              • 1





                I'm very interesting why we need class name injection? What the problem it want to solve, any code example?

                – ZijingWu
                Sep 5 '13 at 8:06








              • 1





                @ZijingWu: Here is one example, would you rather write function f1 or f2? The injected-class-name makes A f2() {return A();} possible.

                – Jesse Good
                Sep 5 '13 at 9:52











              • so injected-class-name is try to make reference class template easier? And even in this case why the base class name also be injected into the scope, which cause a lot of confusing like this post, I see the same question more than once in SO.

                – ZijingWu
                Sep 5 '13 at 10:04
















              4














              You need to add the :: operator. Lookup is finding the injected-class-name instead of the myns::B.



              class D2 : D1 {
              void func(::B b) {}
              };


              11.1p5




              5 [ Note: In a derived class, the lookup of a base class name will
              find the injected-class-name instead of the name of the base class in
              the scope in which it was declared. The injected-class-name might be
              less accessible than the name of the base class in the scope in which
              it was declared. —end note ]
              [Example:




              class A { };
              class B : private A { };
              class C : public B {
              A *p; // error: injected-class-name A is inaccessible
              ::A *q; // OK
              };





              share|improve this answer





















              • 1





                I'm very interesting why we need class name injection? What the problem it want to solve, any code example?

                – ZijingWu
                Sep 5 '13 at 8:06








              • 1





                @ZijingWu: Here is one example, would you rather write function f1 or f2? The injected-class-name makes A f2() {return A();} possible.

                – Jesse Good
                Sep 5 '13 at 9:52











              • so injected-class-name is try to make reference class template easier? And even in this case why the base class name also be injected into the scope, which cause a lot of confusing like this post, I see the same question more than once in SO.

                – ZijingWu
                Sep 5 '13 at 10:04














              4












              4








              4







              You need to add the :: operator. Lookup is finding the injected-class-name instead of the myns::B.



              class D2 : D1 {
              void func(::B b) {}
              };


              11.1p5




              5 [ Note: In a derived class, the lookup of a base class name will
              find the injected-class-name instead of the name of the base class in
              the scope in which it was declared. The injected-class-name might be
              less accessible than the name of the base class in the scope in which
              it was declared. —end note ]
              [Example:




              class A { };
              class B : private A { };
              class C : public B {
              A *p; // error: injected-class-name A is inaccessible
              ::A *q; // OK
              };





              share|improve this answer















              You need to add the :: operator. Lookup is finding the injected-class-name instead of the myns::B.



              class D2 : D1 {
              void func(::B b) {}
              };


              11.1p5




              5 [ Note: In a derived class, the lookup of a base class name will
              find the injected-class-name instead of the name of the base class in
              the scope in which it was declared. The injected-class-name might be
              less accessible than the name of the base class in the scope in which
              it was declared. —end note ]
              [Example:




              class A { };
              class B : private A { };
              class C : public B {
              A *p; // error: injected-class-name A is inaccessible
              ::A *q; // OK
              };






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Sep 5 '13 at 3:37

























              answered Sep 5 '13 at 3:31









              Jesse GoodJesse Good

              38.1k1086141




              38.1k1086141








              • 1





                I'm very interesting why we need class name injection? What the problem it want to solve, any code example?

                – ZijingWu
                Sep 5 '13 at 8:06








              • 1





                @ZijingWu: Here is one example, would you rather write function f1 or f2? The injected-class-name makes A f2() {return A();} possible.

                – Jesse Good
                Sep 5 '13 at 9:52











              • so injected-class-name is try to make reference class template easier? And even in this case why the base class name also be injected into the scope, which cause a lot of confusing like this post, I see the same question more than once in SO.

                – ZijingWu
                Sep 5 '13 at 10:04














              • 1





                I'm very interesting why we need class name injection? What the problem it want to solve, any code example?

                – ZijingWu
                Sep 5 '13 at 8:06








              • 1





                @ZijingWu: Here is one example, would you rather write function f1 or f2? The injected-class-name makes A f2() {return A();} possible.

                – Jesse Good
                Sep 5 '13 at 9:52











              • so injected-class-name is try to make reference class template easier? And even in this case why the base class name also be injected into the scope, which cause a lot of confusing like this post, I see the same question more than once in SO.

                – ZijingWu
                Sep 5 '13 at 10:04








              1




              1





              I'm very interesting why we need class name injection? What the problem it want to solve, any code example?

              – ZijingWu
              Sep 5 '13 at 8:06







              I'm very interesting why we need class name injection? What the problem it want to solve, any code example?

              – ZijingWu
              Sep 5 '13 at 8:06






              1




              1





              @ZijingWu: Here is one example, would you rather write function f1 or f2? The injected-class-name makes A f2() {return A();} possible.

              – Jesse Good
              Sep 5 '13 at 9:52





              @ZijingWu: Here is one example, would you rather write function f1 or f2? The injected-class-name makes A f2() {return A();} possible.

              – Jesse Good
              Sep 5 '13 at 9:52













              so injected-class-name is try to make reference class template easier? And even in this case why the base class name also be injected into the scope, which cause a lot of confusing like this post, I see the same question more than once in SO.

              – ZijingWu
              Sep 5 '13 at 10:04





              so injected-class-name is try to make reference class template easier? And even in this case why the base class name also be injected into the scope, which cause a lot of confusing like this post, I see the same question more than once in SO.

              – ZijingWu
              Sep 5 '13 at 10:04











              0














              The default access specifier when inheriting a class/struct is "private", so your problem is this:



              class D1 : B {};
              class D2 : D1 {};


              Add the keyword "public" and voila.



              http://ideone.com/T5RpUM



              namespace myns {
              class B {};
              }

              using namespace myns;

              class D1 : public B {};

              class D2 : public D1 {
              void func(B b) {}
              };

              int main() {
              D2 d();
              }





              share|improve this answer




























                0














                The default access specifier when inheriting a class/struct is "private", so your problem is this:



                class D1 : B {};
                class D2 : D1 {};


                Add the keyword "public" and voila.



                http://ideone.com/T5RpUM



                namespace myns {
                class B {};
                }

                using namespace myns;

                class D1 : public B {};

                class D2 : public D1 {
                void func(B b) {}
                };

                int main() {
                D2 d();
                }





                share|improve this answer


























                  0












                  0








                  0







                  The default access specifier when inheriting a class/struct is "private", so your problem is this:



                  class D1 : B {};
                  class D2 : D1 {};


                  Add the keyword "public" and voila.



                  http://ideone.com/T5RpUM



                  namespace myns {
                  class B {};
                  }

                  using namespace myns;

                  class D1 : public B {};

                  class D2 : public D1 {
                  void func(B b) {}
                  };

                  int main() {
                  D2 d();
                  }





                  share|improve this answer













                  The default access specifier when inheriting a class/struct is "private", so your problem is this:



                  class D1 : B {};
                  class D2 : D1 {};


                  Add the keyword "public" and voila.



                  http://ideone.com/T5RpUM



                  namespace myns {
                  class B {};
                  }

                  using namespace myns;

                  class D1 : public B {};

                  class D2 : public D1 {
                  void func(B b) {}
                  };

                  int main() {
                  D2 d();
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 5 '13 at 5:28









                  kfsonekfsone

                  19k22655




                  19k22655






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18627191%2fclass-inaccessible-after-inheritance%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas