2010-04-23 20 views
2
//using namespace std; 

using std::ifstream; 

using std::ofstream; 

using std::cout; 

class Dog 
{ 

    friend ostream& operator<< (ostream&, const Dog&); 

    public: 
     char* name; 
     char* breed; 
     char* gender; 

     Dog(); 
     ~Dog(); 

}; 

< < 연산자를 오버로드하려고합니다. 좋은 코딩을 연습하려고합니다. 그러나 네임 스페이스 std를 사용하여 주석을 제거하지 않으면 내 코드가 컴파일되지 않습니다. 나는이 오류가 계속 발생하고 나는 모른다. 메신저 g ++ 컴파일러를 사용하여.연산자 << 과부하

Dog.h:20: error: ISO C++ forbids declaration of ‘ostream’ with no type 
Dog.h:20: error: ‘ostream’ is neither function nor member function; cannot be declared friend. if i add line using std::cout; then i get this error. 
Dog.h:21: error: ISO C++ forbids declaration of ‘ostream’ with no type. 

누군가가 네임 스페이스 표준을 사용하여 함께 < < 연산자를 오버로드 나에게 올바른 방법을 말할 수있다;

답변

3

using std::ostream 대신 using std::ofstream이 있으니 어떤 문자가 ostream인지 알 수 없습니다.

<ostream>도 입력해야합니다.

그래도 사용할 이유가 없습니다. using anything;

friend std::ostream& operator<< (std::ostream&, const Dog&); 
0

using 키워드는 당신이 접두사없이 뭔가에 액세스 할 수 있도록하는 것을 의미한다 (이것은 헤더 파일 인 경우 다른 파일의 글로벌 네임 스페이스를 오염 피하기 위해, 특히를) 그냥 네임 스페이스로 이름을 정규화해야 그것의 네임 스페이스와 함께. orther 단어에서 using std::ofstream;std::ofstreamofstream으로 액세스 할 수있게 해줍니다.

#include <iostream>도 필요합니다. 그래서 컴파일러는 ostream이 무엇인지 알지 못합니다. 친구 코드를 friend std::ostream& operator<< (std::ostream&, const Dog&);으로 변경하고 using을 헤더에 넣을 나쁜 형식이므로 모든 using 항목을 제거하십시오. 그러면 괜찮습니다.