C++ 对另一个类中的类使用重载运算符
我有一个带有重载插入运算符的类,它在我的驱动程序中工作:C++ 对另一个类中的类使用重载运算符,c++,class,c++11,operator-overloading,C++,Class,C++11,Operator Overloading,我有一个带有重载插入运算符的类,它在我的驱动程序中工作: #include <iostream> #include <cstdlib> #include "xml_attribute.h" int main(){ using namespace std; XML_AttributeT a("name","value"); cout << a << endl; return EXIT_SUCCESS; } #包
#include <iostream>
#include <cstdlib>
#include "xml_attribute.h"
int main(){
using namespace std;
XML_AttributeT a("name","value");
cout << a << endl;
return EXIT_SUCCESS;
}
#包括
#包括
#包括“xml_attribute.h”
int main(){
使用名称空间std;
XML_属性a(“名称”、“值”);
库特
对“operator”的未定义引用从技术上讲,这是插入运算符,而不是提取运算符。更严格地说,它是左移位运算符。此外,一次使用#pragma和include-guard都是冗余的。前者只是一种更快的非标准方法。@chris@chris:它不是冗余的,添加#pragma-onc如果编译器支持的话,e
可以利用更快的编译速度。@Xploit,啊,我不知道编译器可以利用它进行优化。从现在起,我将把这两者都放进去。我想你不能在上面的源代码列表中包含xml_attribute.h/cpp吗?
#pragma once
#ifndef XML_ELEMENTT_H
#define XML_ELEMENTT_H
#include <string>
#include <vector>
#include <iostream>
#include "xml_attribute/xml_attribute.h"
struct XML_ElementT{
std::string start_tag;
std::vector<XML_AttributeT> attributes;
bool single_tag;
//Constructors
explicit XML_ElementT(std::string const& start_tag, std::vector<XML_AttributeT> const& attributes, bool const& single_tag);
explicit XML_ElementT(void);
//overloaded extraction operator
friend std::ostream& operator << (std::ostream &out, XML_ElementT const& element);
};
#endif
#include "xml_element.h"
//Constructors
XML_ElementT::XML_ElementT(std::string const& tag_, std::vector<XML_AttributeT> const& attributes_, bool const& single_tag_)
: start_tag{tag_}
, attributes{attributes_}
, single_tag{single_tag_}
{}
XML_ElementT::XML_ElementT(void){}
//overloaded extraction operator
std::ostream& operator << (std::ostream &out, XML_ElementT const& element){
for (std::size_t i = 0; i < element.attributes.size()-1; ++i){
out << element.attributes[i]; //<- Does not work
}
return out;// << element.attributes[element.attributes.size()-1];
}