我是C ++的新手,我尝试实现自己的链接列表,而在以下代码中遇到了此问题。
main.cpp
#include <iostream>
#include <string>
#include "headers/heder.h"
using namespace std;
int main() {
MyLinkedList<int> integ = new MyLinkedList<int>(3);
MyLinkedList<string> third ("Cookie", nullptr);
MyLinkedList<string> second ("likes", &third);
MyLinkedList<string> first ("James", &second);
MyLinkedList<string>* selected = &first;
while (selected->getNext() != NULL){
cout << selected->getNext()->getValue() << " ";
selected = selected->getNext();
}
return 0;
}
heder.h
template <class T>
class MyLinkedList {
T value;
MyLinkedList* next;
public:
MyLinkedList *getNext() const;
void setNext(MyLinkedList *next);
T getValue() const;
void setValue(T value);
MyLinkedList(T value, MyLinkedList *next);
MyLinkedList(T value);
MyLinkedList(MyLinkedList *next);
};
heder.cpp
template<class T>
T MyLinkedList<T>::getValue() const {
return value;
}
template<class T>
void MyLinkedList<T>::setValue(T value) {
MyLinkedList::value = value;
}
template<class T>
MyLinkedList<T> *MyLinkedList<T>::getNext() const {
return next;
}
template<class T>
void MyLinkedList<T>::setNext(MyLinkedList *next) {
MyLinkedList::next = next;
}
template<class T>
MyLinkedList<T>::MyLinkedList(T value, MyLinkedList *next):value(value), next(next) {}
template<class T>
MyLinkedList<T>::MyLinkedList(T value):value(value) { next = NULL; }
template<class T>
MyLinkedList<T>::MyLinkedList(MyLinkedList *next): next(next) { &value = NULL; }
为什么我会得到他的错误:
CMakeFiles\untitled.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/mstele/CLionProjects/untitled/main.cpp:24: undefined reference to `MyLinkedList<int>::MyLinkedList(int)'
C:/Users/mstele/CLionProjects/untitled/main.cpp:24: undefined reference to `MyLinkedList<int>::MyLinkedList(MyLinkedList<int>*)'
C:/Users/mstele/CLionProjects/untitled/main.cpp:27: undefined reference to `MyLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::MyLinkedList(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, MyLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >*)'
C:/Users/mstele/CLionProjects/untitled/main.cpp:28: undefined reference to `MyLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::MyLinkedList(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, MyLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >*)'
C:/Users/mstele/CLionProjects/untitled/main.cpp:29: undefined reference to `MyLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::MyLinkedList(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, MyLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >*)'
C:/Users/mstele/CLionProjects/untitled/main.cpp:33: undefined reference to `MyLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::getNext() const'
C:/Users/mstele/CLionProjects/untitled/main.cpp:34: undefined reference to `MyLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::getNext() const'
C:/Users/mstele/CLionProjects/untitled/main.cpp:34: undefined reference to `MyLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::getValue() const'
C:/Users/mstele/CLionProjects/untitled/main.cpp:35: undefined reference to `MyLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::getNext() const'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\untitled.dir\build.make:100: untitled.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/untitled.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/untitled.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: untitled] Error 2
我在stackoverflow上没有找到任何类似的答案,因此,如果有人可以帮助我,我将非常高兴。
答案 0 :(得分:0)
您必须将heder.cpp的全部内容移到heder.h中。模板定义的使用位置(即main.cpp中)必须是可见的。这与非模板功能不同。