In C++, I am attempting to write a templated version of operator<<
for std::set
so that I don't have to write one for each permutation of contained value types.
I have defined my header as utils.h
:
#ifndef UTILS
#define UTILS
#include <ostream>
#include <set>
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::set<T> &tgt);
template <typename T>
inline std::ostream &operator<<(std::ostream &os,
const std::set<T> *tgt) {
return os << *tgt;
}
#endif
The implementation is in utils.cpp
:
#include "utils.h"
template <typename T>
std::ostream &operator<<(std::ostream &os, const std::set<T> &tgt) {
os << "{";
bool first = true;
for (typename std::set<T>::const_iterator i = tgt.begin(); i != tgt.end();
i++) {
if (!first) {
os << ", ";
} else {
first = false;
}
os << i;
}
os << "}";
return os;
}
My attempt to use it in cout.cpp
:
#include <iostream>
#include <set>
#include "utils.h"
class Value {
public:
const int value;
Value(const int value) : value(value) {}
};
std::ostream &operator<<(std::ostream &os, const Value &tgt) {
os << "Value(" << tgt.value << ")";
return os;
}
inline std::ostream &operator<<(std::ostream &os, const Value *tgt) {
return os << *tgt;
}
int main(const int argc, const char **argv) {
std::cout << argc << std::endl;
std::cout << *argv << std::endl;
Value v(10);
std::cout << v << std::endl;
Value *w = &v;
std::cout << *w << std::endl;
std::set<const Value *> vs;
vs.insert(w);
vs.insert(&v);
Value x = Value(12);
vs.insert(&x);
std::cout << vs << std::endl;
std::set<const Value *> *p = &vs;
std::cout << p << std::endl;
return 0;
}
I am attempting to compile & run it with:
clang++ -c utils.cpp
clang++ -o cout cout.cpp utils.o
./cout
The error that clang++
produces is:
tmp/cout-6e3988.o: In function `main':
cout.cpp:(.text+0x24d): undefined reference to `std::ostream& operator<< <Value const*>(std::ostream&, std::set<Value const*, std::less<Value const*>, std::allocator<Value const*> > const&)'
/tmp/cout-6e3988.o: In function `std::ostream& operator<< <Value const*>(std::ostream&, std::set<Value const*, std::less<Value const*>, std::allocator<Value const*> > const*)':
cout.cpp:(.text._ZlsIPK5ValueERSoS3_PKSt3setIT_St4lessIS5_ESaIS5_EE[_ZlsIPK5ValueERSoS3_PKSt3setIT_St4lessIS5_ESaIS5_EE]+0x19): undefined reference to `std::ostream& operator<< <Value const*>(std::ostream&, std::set<Value const*, std::less<Value const*>, std::allocator<Value const*> > const&)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I understand that it's not finding the appropriate operator<<
implementation during linking, but I have no idea how to go about resolving this issue.
Thanks.