我有这个代码:
template<typename T>
class Listoid{
private:
std::vector<T> list;
public:
typedef typename std::vector<T>::iterator iterator;
iterator begin() {return list.begin();}
iterator end() {return list.end();}
public:
Listoid(T t) {
list.push_back(t);
}
const T operator [](int i){
return list[i];
}
void addElem(T ne){
list.push_back(ne);
}
friend T cons(T new_elem, Listoid<T> list);
};
template<typename T>
Listoid<T> cons(T new_elem, Listoid<T> list){
Listoid<T> new_list(new_elem);
for(typename Listoid<T>::iterator it = list.begin(), e = list.end();
it != e; ++it){
new_list.addElem(*it);
}
return new_list;
}
int main(){
Listoid<int> lista(312);
lista.addElem(22);
Listoid<int> lista2 = cons(21, lista);
return EXIT_SUCCESS;
}
但我无法编译它;我收到以下错误:
/tmp/listoid-3kYCmd.o: In function `main':
listoid.cpp:(.text+0xda): undefined reference to `cons(int, Listoid<int>)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
也许它真的很简单,但我无法解决它。有人可以帮忙吗?