Reputation: 37834
I have a class with an overloaded insertion operator and it works in my driver:
#include <iostream>
#include <cstdlib>
#include "xml_attribute.h"
int main(){
using namespace std;
XML_AttributeT a("name","value");
cout << a << endl;
return EXIT_SUCCESS;
}
Outputs: name="value"
works.
I want to leverage this capability in another class that includes this class:
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];
}
Error:
undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, XML_AttributeT const&)
how can I get this to work?
Upvotes: 0
Views: 220
Reputation: 208353
undefined reference to `operator<<(std::basic_ostream<...>&, XML_AttributeT const&)
That error means that the compiler has parsed the code appropriately and that you have a declaration for the operator with the signature above. But, the linker has not found the definition of that function in your program. This can be caused by different things, like failing to link the library or the .o where the operator is defined.
Upvotes: 2