我有一个带有重载插入运算符的类,它在我的驱动程序中工作:
#include <iostream>
#include <cstdlib>
#include "xml_attribute.h"
int main(){
using namespace std;
XML_AttributeT a("name","value");
cout << a << endl;
return EXIT_SUCCESS;
}
输出:name="value"
有效。
我想在另一个包含此类的类中利用此功能:
header
#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
cpp
#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];
}
错误:
undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, XML_AttributeT const&)
我怎样才能让它工作?