在 C++ 中,我正在尝试编写 for 的模板版本,operator<<
这样std::set
我就不必为包含的值类型的每个排列编写一个。
我已将我的标题定义为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
实施在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;
}
我尝试使用它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;
}
我正在尝试编译并运行它:
clang++ -c utils.cpp
clang++ -o cout cout.cpp utils.o
./cout
产生的错误clang++
是:
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)
我知道operator<<
在链接过程中没有找到合适的实现,但我不知道如何解决这个问题。
谢谢。