map.hpp
ファイルに内部クラスを含む次のテンプレートがあります。
template<typename Key_T, typename Mapped_T>
class Map {
// public members of Map ...
class Iterator {
//public members of Iterator ...
friend bool operator!=(const Iterator &i, const Iterator &j) {
return (i.link != j.link);
}
// private members of iterator ...
Node * link;
};
};
#include "map.hxx" //implementation file for member methods is separate
私はmain.cpp
以下を呼び出しますが、これまでのところすべて正常に動作しています:
Map<int, int> x;
// bunch of insertions ...
for (auto it = x.begin; it != x.end(); ++it) {
// Do something with it ...
}
map.hpp
ただし、フレンド関数をファイルからmap.hxx
他の実装を含むファイルに移動したいと考えています。
Q: フリー関数を.hxx
ファイルに移動することは可能ですか? どのようにしますか?
Iterator クラスで関数をフレンドとして宣言するのにうんざりし、実装ファイルで次のことを行いました。
template<typename Key_T, typename Mapped_T>
bool operator!=(const typename Map<Key_T, Mapped_T>::Iterator & i,
const typename Map<Key_T, Mapped_T>::Iterator & j) {
return (i.link != j.link);
}
ただし、次のように失敗しました:
$clang++ -std=c++11 -stdlib=libc++ -Wall -Wextra -g main.cpp
Undefined symbols for architecture x86_64:
"shiraz::operator!=(shiraz::Map<int, int>::Iterator const&, shiraz::Map<int, int>::Iterator const&)", referenced from:
_main in main-3oCRAm.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ありがとう!