다음 코드가 컴파일되지 않는 이유를 알 수 없습니다. 구문은 다른 연산자 오버로드와 같습니다. < < 과부하가 필요하다는 제한이 있습니까? 그렇다면 왜? 어떤 도움을 주셔서 감사합니다.왜 이렇게하지 않습니까? << 과부하 컴파일
이 작동하지 않습니다 - 최초의 형태가 temp << std::cout
과부하 때문에
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
class Test
{
public:
explicit Test(int var):
m_Var(var)
{ }
friend std::ostream& operator<< (std::ostream& stream, Test& temp);
private:
int m_Var;
};
std::ostream& operator<< (std::ostream& stream, Test& temp)
{
return stream << temp.m_Var;
};
int _tmain(int argc, _TCHAR* argv[])
{
Test temp(5);
std::cout << temp;
return 0;
}
물론 이것은 속임수입니다. http://stackoverflow.com/questions/236801/should-operator-be-implemented-as-a-friend-or-as-a-member-function, 예를 들어 –
@ Neil 인 경우, Google에서이 답변에 대한 높고 낮음을 검색하여 찾을 수 없습니다. 친구와 함께 구현하는 방법이 궁금하지 않았지만, 왜 처음에는 구현할 수 없었습니다. – Steve