2012-04-22 4 views
1

내 클래스에 연산자 < <을 사용하려고합니다. 클래스친구 연산자를 사용하여 컴파일러 오류가 발생했습니다. <<

정의 : 클래스 외부

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

정의 :

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::negativelongint::arraylongint::digtochar 개인이며, 공개 여기에 코드입니다. 봤 많은 예제에서 나는 개인 회원이 친구 사업자에서 사용할 수있는 것을 알 수 있습니다,하지만 난 오류 제공 :

../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 

왜? 내가 뭘 잘못 했니?

UPD. 최소 코드 :

// 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; 
} 

답장을 보내 주셔서 감사합니다.

+1

정상 작동합니다. 문제를 재현하는 최소한의 코드를 게시하십시오. –

+3

함수에서'out'을 리턴해야합니다. – chris

+0

@LuchianGrigore 확인. –

답변

5

friend 선언은 네임 스페이스 안에 있지만 함수의 정의는 아닙니다. 따라서 일치하지 않으며 친구 권한이 활성화되지 않습니다.

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; 
    } 
} 

같은 네임 스페이스 내에서 정의를 이동

그리고 작업을 시작해야한다.

관련 문제