I have been banging my head against this for a couple of days, looking it up and also looking for similar code in open source projects: can't really find what I'm doing incorrectly.
Essentially, given the code below (distilled to its essence):
template <typename T>
class Node {
T value_;
public:
Node(const T& value) : value_(value) {}
T const value() const { return value_; }
friend
std::ostream& operator <<(std::ostream& out, const Node<T>& node);
Node<T> operator +(const Node<T>& other) {
return Node(value() + other.value());
}
};
template <typename T>
std::ostream& operator <<(std::ostream& out, const Node<T>& node) {
return out << node.value();
}
when used in code such as this:
int main(int argc, char* argv[]) {
Node<string> node("node X");
cout << node << endl;
Node<int> three(3);
cout << three << endl;
return EXIT_SUCCESS;
}
I get the following linker error:
Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const&)", referenced from:
_main in StlPractice.cpp.o
"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Node<int> const&)", referenced from:
_main in StlPractice.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
As far as I can tell, the above is all legal C++11 code; the template is well-defined, and yet, it seems to somehow escape the ability of the linker to find it.
This is built using cmake on OS X:
cmake_minimum_required(VERSION 3.3)
project(simple_template)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES src/StlPractice.cpp)
add_executable(StlPractice ${SOURCE_FILES})
What gives?
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire