Can't overload i/o operators for private enum in namespaceOverloading operator<< for a private enumCast int to enum in C#How do I enumerate an enum in C#?Should 'using' directives be inside or outside the namespace?What is the preferred syntax for defining enums in JavaScript?How to get an enum value from a string value in Java?Get int value from enum in C#Why is “using namespace std;” considered bad practice?What is the “-->” operator in C++?Comparing Java enum members: == or equals()?What are the basic rules and idioms for operator overloading?

How did Einstein know the speed of light was constant?

How did שְׁלֹמֹה (shlomo) become Solomon?

What is the addition in the re-released version of Avengers: Endgame?

Advice for making/keeping shredded chicken moist?

Has chattel slavery ever been used as a criminal punishment in the USA since the passage of the Thirteenth Amendment?

What happens if the limit of 4 billion files was exceeded in an ext4 partition?

Did Snape really give Umbridge a fake Veritaserum potion that Harry later pretended to drink?

Should I cheat if the majority does it?

Interview Question - Card betting

Show that there are infinitely more problems than we will ever be able to compute

What is the maximum amount of diamond in one Minecraft game?

List comprehensions in Mathematica?

How would an Amulet of Proof Against Detection and Location interact with the Comprehend Languages spell?

Term for a character that only exists to be talked to

Implementing absolute value function in c

Can you use a weapon affected by Heat Metal each turn if you drop it in between?

Boss has banned cycling to work because he thinks it's unsafe

Are there advantages in writing by hand over typing out a story?

What's the big deal about the Nazgûl losing their horses?

How come having a Deathly Hallow is not a big deal?

Is it bad to suddenly introduce another element to your fantasy world a good ways into the story?

Why does mean tend be more stable in different samples than median?

Why is there paternal, for fatherly, fraternal, for brotherly, but no similar word for sons?

What is the difference between a historical drama and a period drama?



Can't overload i/o operators for private enum in namespace


Overloading operator<< for a private enumCast int to enum in C#How do I enumerate an enum in C#?Should 'using' directives be inside or outside the namespace?What is the preferred syntax for defining enums in JavaScript?How to get an enum value from a string value in Java?Get int value from enum in C#Why is “using namespace std;” considered bad practice?What is the “-->” operator in C++?Comparing Java enum members: == or equals()?What are the basic rules and idioms for operator overloading?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have a private enum in a class within a namespace. I'm trying to overload the I/O operators, but all I get is the compiler complaining about the Enum being private. The solution from this post did nothing to help me. Here is an isolated version of my problem.



TestClass.h



#include <iostream>
namespace Test

class TestClass

enum Enum : unsigned int a = 0, b;
friend std::ostream& operator<<(std::ostream& os, Enum e);
;
std::ostream& operator<<(std::ostream& os, TestClass::Enum e);
;


TestClass.cpp



#include "TestClass.h"
std::ostream& operator<<(std::ostream& os, Test::TestClass::Enum e)

//do it



The compiler complains about this, but does not complain when I remove the class from the namespace, so how do I get this to compile?



I'm using




g++ -c TestClass.h




to compile this










share|improve this question






























    1















    I have a private enum in a class within a namespace. I'm trying to overload the I/O operators, but all I get is the compiler complaining about the Enum being private. The solution from this post did nothing to help me. Here is an isolated version of my problem.



    TestClass.h



    #include <iostream>
    namespace Test

    class TestClass

    enum Enum : unsigned int a = 0, b;
    friend std::ostream& operator<<(std::ostream& os, Enum e);
    ;
    std::ostream& operator<<(std::ostream& os, TestClass::Enum e);
    ;


    TestClass.cpp



    #include "TestClass.h"
    std::ostream& operator<<(std::ostream& os, Test::TestClass::Enum e)

    //do it



    The compiler complains about this, but does not complain when I remove the class from the namespace, so how do I get this to compile?



    I'm using




    g++ -c TestClass.h




    to compile this










    share|improve this question


























      1












      1








      1








      I have a private enum in a class within a namespace. I'm trying to overload the I/O operators, but all I get is the compiler complaining about the Enum being private. The solution from this post did nothing to help me. Here is an isolated version of my problem.



      TestClass.h



      #include <iostream>
      namespace Test

      class TestClass

      enum Enum : unsigned int a = 0, b;
      friend std::ostream& operator<<(std::ostream& os, Enum e);
      ;
      std::ostream& operator<<(std::ostream& os, TestClass::Enum e);
      ;


      TestClass.cpp



      #include "TestClass.h"
      std::ostream& operator<<(std::ostream& os, Test::TestClass::Enum e)

      //do it



      The compiler complains about this, but does not complain when I remove the class from the namespace, so how do I get this to compile?



      I'm using




      g++ -c TestClass.h




      to compile this










      share|improve this question
















      I have a private enum in a class within a namespace. I'm trying to overload the I/O operators, but all I get is the compiler complaining about the Enum being private. The solution from this post did nothing to help me. Here is an isolated version of my problem.



      TestClass.h



      #include <iostream>
      namespace Test

      class TestClass

      enum Enum : unsigned int a = 0, b;
      friend std::ostream& operator<<(std::ostream& os, Enum e);
      ;
      std::ostream& operator<<(std::ostream& os, TestClass::Enum e);
      ;


      TestClass.cpp



      #include "TestClass.h"
      std::ostream& operator<<(std::ostream& os, Test::TestClass::Enum e)

      //do it



      The compiler complains about this, but does not complain when I remove the class from the namespace, so how do I get this to compile?



      I'm using




      g++ -c TestClass.h




      to compile this







      c++ enums namespaces operator-overloading friend






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 19:34







      ABernitt

















      asked Mar 25 at 19:11









      ABernittABernitt

      155 bronze badges




      155 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          3














          The operator in your cpp file is not the friend you declared. The friend is a member of the namespace, because the class it's declared in is a member.



          So wrap the operator definition in the namespace scope too. Or fully qualify the defintion



          std::ostream& Test::operator<<(std::ostream& os, Test::TestClass::Enum e)

          //do it






          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%2f55344919%2fcant-overload-i-o-operators-for-private-enum-in-namespace%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            The operator in your cpp file is not the friend you declared. The friend is a member of the namespace, because the class it's declared in is a member.



            So wrap the operator definition in the namespace scope too. Or fully qualify the defintion



            std::ostream& Test::operator<<(std::ostream& os, Test::TestClass::Enum e)

            //do it






            share|improve this answer





























              3














              The operator in your cpp file is not the friend you declared. The friend is a member of the namespace, because the class it's declared in is a member.



              So wrap the operator definition in the namespace scope too. Or fully qualify the defintion



              std::ostream& Test::operator<<(std::ostream& os, Test::TestClass::Enum e)

              //do it






              share|improve this answer



























                3












                3








                3







                The operator in your cpp file is not the friend you declared. The friend is a member of the namespace, because the class it's declared in is a member.



                So wrap the operator definition in the namespace scope too. Or fully qualify the defintion



                std::ostream& Test::operator<<(std::ostream& os, Test::TestClass::Enum e)

                //do it






                share|improve this answer















                The operator in your cpp file is not the friend you declared. The friend is a member of the namespace, because the class it's declared in is a member.



                So wrap the operator definition in the namespace scope too. Or fully qualify the defintion



                std::ostream& Test::operator<<(std::ostream& os, Test::TestClass::Enum e)

                //do it







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 25 at 19:25

























                answered Mar 25 at 19:20









                StoryTellerStoryTeller

                113k18 gold badges243 silver badges308 bronze badges




                113k18 gold badges243 silver badges308 bronze badges


















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    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%2f55344919%2fcant-overload-i-o-operators-for-private-enum-in-namespace%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

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript