xml_attribute.h오버로드 된 연산자 << 출력 bool 값. 왜?
#pragma once
#ifndef XML_ATTRIBUTET_H
#define XML_ATTRIBUTET_H
#include <string>
#include <iostream>
struct XML_AttributeT{
std::string tag;
std::string value;
//constructors
explicit XML_AttributeT(std::string const& tag, std::string const& value);
explicit XML_AttributeT(void);
//overloaded extraction operator
friend std::ostream& operator << (std::ostream &out, XML_AttributeT const& attribute);
};
#endif
xml_attribute.cpp
#include "xml_attribute.h"
//Constructors
XML_AttributeT::XML_AttributeT(std::string const& tag_, std::string const& value_)
: tag{tag_}
, value{value_}
{}
XML_AttributeT::XML_AttributeT(void){}
//overloaded extraction operator
std::ostream& operator << (std::ostream &out, XML_AttributeT const attribute){
return out << attribute.tag << "=" << attribute.value;
}
driver.cpp
#include <iostream>
#include <cstdlib>
#include "xml_attribute.h"
int main(){
using namespace std;
XML_AttributeT a();
cout << a << endl;
return EXIT_SUCCESS;
}
드라이버의 출력은 '1'하지만 그것은 '는 되길 = '표시.
참조를 출력하는 이유는 무엇입니까?
XML_AttributeT a();
에서 XML_AttributeT a;
으로 변경하면 컴파일되지 않습니다.
내가 뭘 잘못 했니?
아, 가장 번거로운 구문 분석. C++ 11을 사용하고 있기 때문에'()'대신'{}'을 사용하여 초기화를 통일 할 수 있습니다. – chris
'operator <<'의 두 번째 인수는'XML_AttributeT const' (즉 값으로 전달)로 선언되지만 friend 선언은 XML_AttributeT const & (참조로 전달)를 사용합니다. 그들은 동일해야합니다 (나는 참조로 통과하는 것이 좋습니다). – jogojapan
@jogojapan 우, 좋은 눈. 결정된. –