У меня есть следующий шаблон с внутренними классами в 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
файл, который содержит другую реализацию.
В: Можно ли переместить свободную функцию в файл .hxx
и как?
Я устал объявить функцию как друга в классе итератора и сделал следующее в файле реализации:
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)
Спасибо!
Возможный дубликат: http://stackoverflow.com/q/495021/596781 –
Почему вы спрашиваете о «синтаксисе», если у вас есть * линкер * ошибка? –
Это может быть дубликат [этого вопроса] (http://stackoverflow.com/a/29062048/420683). Пожалуйста, покажите нам свою декларацию друга. – dyp