2011-01-17 10 views
1

fstream을 사용 중이며 오류가 발생했습니다.iostream에서 C++ 컴파일 오류가 발생했습니다.

class CLog 
{ 
    void printOn(std::ostream& dbg) const; 
} 

void operator>>(const CLog& s, std::ofstream& dbg) 
{ 
    s.printOn(dbg); 
} 

하지만 컴파일 할 때 다음과 같은 오류 있어요 : : ofstream이 불가능 왜 ostream에에서 상속

error C2664: 'printOn' : cannot convert parameter 1 from 
'class std::basic_ofstream<char,struct std::char_traits<char> >' to 
'class std::basic_ostream<char,struct std::char_traits<char> > &' 
A reference that is not to 'const' cannot be bound to a non-lvalue 

생각을 여기에

내가 가진 무엇인가? 출력 연산자의

+0

Arggg! 누락 된'#include '을 발견했습니다. 컴파일러의 어리석은 결과물. 모두 고마워요 – mathk

답변

2

이 :) printOn 공개를 확인하고 fstream 헤더를 포함한다.

#include <fstream> 
class CLog 
{ 
public: 
    void printOn(std::ostream& dbg) const 
    { 

    } 
}; 

std::ofstream & operator<<(std::ofstream& dbg, const CLog& s) 
{ 
    s.printOn(dbg); 
} 
+0

printOn이 (가) 공개 fstream was missing thanks :)입니다. 그런데 왜 이런 상황에서는 컴파일러 출력이 쓸모가 없다. – mathk

+0

그래, 나 혼란스러워했다. :) – UmmaGumma

+0

연산자의 매개 변수 순서를 바꿔 놓았던 점에 주목할 가치가있다. 추출 연산자는 스트림이 왼쪽에 있어야한다. 만약 당신이 실제로 그것을 연산자 <<로 지정했다면, 매개 변수의 순서는 정확합니다. 또 다른 대답은 – lefticus

6

더 정확한 선언이 따르고

감사 :

std::ostream& operator << (std::ostream& dbg, const CLog& s) 
{ 
    s.printOn(dbg); 
    return dbg; 
} 
+0

연산자가되어야합니다 >> – Ferruccio

+0

그리고 당신은 CLog 클래스에서 친구 작업으로 선언 할 수 있습니다. –

+1

그래도 작동하지 않았다. – mathk

1

나는 문제를 재현 할 수 있도록 완벽한 코드를 게시 좋을 것. 나는 아무도 안했습니다 :

#include <fstream> 

class CLog 
{ 
public: 
    void printOn(std::ostream& dbg) const; 
}; 

void operator>>(const CLog& s, std::ofstream& dbg) 
{ 
    s.printOn(dbg); 
} 
관련 문제