Reputation: 4323
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.
Upvotes: 1
Views: 176
Reputation: 283634
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.
Upvotes: 5