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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:11:38+00:00 2026-06-03T03:11:38+00:00

Everything was fine until I moved my objects to a namespace. And now the

  • 0

Everything was fine until I moved my objects to a namespace. And now the compiler claims that my Color attributes are private.

I thought the whole point of friends was to share encapsulated information with those a class befriends.

Color.h

friend ostream & operator << (ostream& output, const st::Color& color);

Color.cpp:

 ostream & operator <<(ostream& output, const st::Color& color) {

    output << "Colors:\nalpha\t: " << color.a << "\nred\t: "  << color.r << "\ngreen\t: " << color.g
            << "\nblue\t: " << color.b <<  "\nvalue\t: " << color.color();

    return output;
}

error:

Color.h||In function 'std::ostream& operator<<(std::ostream&, const st::Color&)':|
Color.h|52|error: 'unsigned char st::Color::a' is private|
Color.cpp|15|error: within this context|
Color.h|49|error: 'unsigned char st::Color::r' is private|
Color.cpp|15|error: within this context|
Color.h|51|error: 'unsigned char st::Color::g' is private|
Color.cpp|15|error: within this context|
Color.h|50|error: 'unsigned char st::Color::b' is private|
Color.cpp|16|error: within this context|
||=== Build finished: 8 errors, 0 warnings (0 minutes, 1 seconds) ===|

So what is the deal?
I’m using Code::Blocks as my IDE. And it won’t even show any properties or methods when I use the dot operator on the “color” parameter. This is obviously a sign of something going wrong…somewhere.

I’ve taken the friend operator overloading out and it compiles just fine. No error elsewhere.
What gives?

It’s declared as follows:

namespace st{

class Color {

    friend ostream & operator << (ostream& output, const st::Color& color);
 public:
     ....
 private:
    .....

};
};

Edit:

In my CPP I’ve now done this:

namespace st{
ostream & st::operator <<(ostream& output, const st::Color& color) {

    output << "Colors:\nalpha\t: " << color.a << "\nred\t: "  << color.r << "\ngreen\t: " << color.g
            << "\nblue\t: " << color.b <<  "\nvalue\t: " << color.color();

    return output;
}
}

st::Color::Color() {

    reset();
}

st::Color::Color(const Color& orig) {

    a = orig.a;
    r = orig.r;
    g = orig.g;
    b = orig.b;
}

void st::Color::reset() {
    a = 0;
    r = 0;
    g = 0;
    b = 0;
}
... etc
}

No compile errors, but is it normal for such a situation to use the namespace again in the header? Or is this completely off from what I should be doing?

Edit:
@Rob thanks for your input as well!

  • 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-03T03:11:39+00:00Added an answer on June 3, 2026 at 3:11 am

    You need to declare and define your operators in the same namespace as the object as well. They will still be found through Argument-Dependent-Lookup.

    A usual implementation will look like this:

    /// header file
    namespace foo {
       class A
       {
        public:
        A();
    
        private:
        int x_;
        friend std::ostream& operator<<(std::ostream& o, const A& a);
        };
    
        std::ostream& operator<<(std::ostream& o, const A& a);
    } // foo
    
    // cpp file
    namespace foo {
         A::A() : x_(23) {}
    
         std::ostream& operator<<(std::ostream& o, const A& a){
         return o << "A: " << a.x_;
        }
    } // foo
    
    
    int main()
    {
      foo::A a;
      std::cout << a << std::endl;
      return 0;
    }
    

    Edit

    It seems that you are not declarin your operator<< in the namespace and are also defining it outside of the namespace. I’ve adjusted the code.

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

Sidebar

Related Questions

I'm declaring a String array of [2][5]. Up until that point everything is fine.
today I started working with DirectX(D3D9), everything went fine until I created a Static-mesh
Everything is working fine until I call the saveFile method (shown below) to write
I was using grails 1.3.4 for my project and everything was working fine until
I am using Eclipse Helios for Java. Everything was working fine until recently. The
I'm running fabric (Django deployment to apache) and everything seems to work fine until
Everything's fine building in xcode and testing the app until I built in for
Until yesterday everything was fine to my localhost, but starting yesterday, localhost is not
I started using Zend_Paginator, it works everything fine but I noticed that there is
I used mouseup/down/move to manually implement jQuery.draggable functionality. Everything was working fine until I

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.