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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:44:28+00:00 2026-06-02T15:44:28+00:00

I’m trying to use operator<< for my class. Here’s the code: Definition in class:

  • 0

I’m trying to use operator<< for my class.
Here’s the code:

Definition in class:

friend std::ostream& operator<<(std::ostream& out, const longint& num);

Definition outside the class:

std::ostream& operator<<(std::ostream& out, const longint& num_) {
    if (num_.negative())
        out << '-';
    longint::const_iterator it = num_.array.end();
    --it;
    longint::const_iterator finish = num_.array.begin();
    --finish;
    while (it != finish) {
        out << num_.digtochar(*it);
        --it;
    }
}

longint::negative is public, longint::array and longint::digtochar are private. In a lot of googled examples I can see that private members can be used in friend operators, but I give errors:

../long_arithm.h:57:20: error: «std::list<signed char> long_arithm::longint::array» is private
../long_arithm.cpp:94:36: error: in this context
../long_arithm.h:57:20: error: «std::list<signed char> long_arithm::longint::array» is private
../long_arithm.cpp:96:40: error: in this context
../long_arithm.h:44:9: error: «long_arithm::schar long_arithm::longint::digtochar(long_arithm::schar) const» is protected
../long_arithm.cpp:99:28: error: in this context

Why? What I make wrong?

UPD. Minimal code:

// long_arithm.h
#ifndef LONG_ARITHM_H_
#define LONG_ARITHM_H_

#include <iostream>
#include <list>

namespace long_arithm {

    typedef signed char schar;

    class longint {
    public:
        typedef std::list<schar>::const_iterator const_iterator;

        inline bool negative() const { return minusSign; }

        friend std::ostream& operator<<(std::ostream& out, const longint& num);

        enum { error_char = 127 };

    protected:
        schar digtochar(schar num) const;

    private:
        bool minusSign;
        std::list<schar> array;
    };
};

// long_arithm.cpp
#include "long_arithm.h"
#include <iostream>

using namespace long_arithm;

schar longint::digtochar(schar num) const {
    switch (num) {
        case 0: return '0';
        case 1: return '1';
        case 2: return '2';
        case 3: return '3';
        case 4: return '4';
        case 5: return '5';
        case 6: return '6';
        case 7: return '7';
        case 8: return '8';
        case 9: return '9';
        default: return error_char;
    }
}

std::ostream& operator<<(std::ostream& out, const longint& num_) {
    if (num_.negative())
        out << '-';
    longint::const_iterator it = num_.array.end();
    --it;
    longint::const_iterator finish = num_.array.begin();
    --finish;
    while (it != finish) {
        out << num_.digtochar(*it);
        --it;
    }
    return out;
}

Thank you for answers.

  • 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-02T15:44:32+00:00Added an answer on June 2, 2026 at 3:44 pm

    Your friend declaration is inside a namespace, but the definition of the function isn’t. Therefore they don’t match, and friend privileges aren’t activated.

    Move the definition inside the namespace, like

    namespace long_arithm
    {
        std::ostream& operator<<(std::ostream& out, const longint& num_)
        {
            if (num_.negative())
                out << '-';
            longint::const_iterator it = num_.array.end();
            --it;
            longint::const_iterator finish = num_.array.begin();
            --finish;
            while (it != finish) {
                out << num_.digtochar(*it);
                --it;
            }
            return out;
        }
    }
    

    and it should start working.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.