0

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?

8
  • Technically that's the insertion operator, not the extraction operator. Even more technically, it's the left shift operator. Also, having both #pragma once and the include guard is redundant. The former is just a quicker, non-standard method. Commented Sep 19, 2012 at 2:13
  • 2
    @chris stackoverflow.com/questions/10779262/… Commented Sep 19, 2012 at 2:20
  • 1
    @chris: It's not redundant, adding #pragma once can take advantage of faster compiling speeds if the compiler supports it. Commented Sep 19, 2012 at 2:21
  • @Xploit, Ah, I wasn't aware compilers could make optimizations off of that. I'll be putting both in from now on. Commented Sep 19, 2012 at 2:22
  • I don't supposed you can include xml_attribute.h/cpp in the source lists above ? Commented Sep 19, 2012 at 2:28

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.