"Undefined reference to"函数错误
"Undefined reference to" function error
我得到了一个非常令人沮丧的错误,我已经挣扎了一段时间了。
在我开始将数据类型从int改为使用模板之前,我已经使程序正常工作了。这是我得到的错误:
g++ -c UseList.cpp
g++ -c List.cpp
g++ UseList.o List.o -o UseList
UseList.o: In function `main':
UseList.cpp:(.text+0xee): undefined reference to `List<int>::addtofront(int)'
UseList.cpp:(.text+0x10b): undefined reference to `List<int>::show()'
collect2: ld returned 1 exit status
make: *** [UseList] Error 1
我的代码的相关部分如下。请让我知道这可能是什么。我完全不知道。谢谢!
UseList.cpp:
#include <iostream>
#include <cstdlib>
#include "List.h"
using namespace std;
int main (int argc, char *argv[]) {
cout << "-----------------------------------------" << endl;
cout << "----------------LIST ONE-----------------" << endl;
cout << "-----------------------------------------" << endl;
List<int> *list1;
srand(time(NULL));
cout << "# Add 3 random ints to the front:" << endl;
for(int i=0;i<3;i++){
list1->addtofront(rand() % 10000);
}
list1->show();
}
List.h
#include <iostream>
using namespace std;
#ifndef LIST
#define LIST
template <class L>
class Node {
public:
// constructors and destructors
Node<L>(L data);
Node();
Node<L>(const Node<L> &n);
// components of a node
L data;
Node *next;
Node *prev;
};
template <class L>
class List
{
private:
Node<L> *first;
Node<L> *last;
int num_elements;
public:
// constructors and destructors
List();
~List();
List<L>(const List<L> &l);
List *addtofront(L);
void show();
[...] // unnecessary code removed
};
// #include "List.cpp"
#endif
List.cpp
#include "List.h"
using namespace std;
// -------------------------------------
// ------------Node class---------------
// -------------------------------------
template <class L>
Node<L>::Node(L data_input) {
data = data_input;
next = 0;
prev = 0;
}
template <class L>
Node<L>::Node() {}
template <class L>
Node<L>::Node(const Node<L> &n) {
data = n.data;
next = n.next;
prev = n.prev;
}
// -------------------------------------
// ------------List class---------------
// -------------------------------------
template <class L>
List<L>::List() {
first = NULL;
last = NULL;
num_elements = 0;
}
template <class L>
// Appends data to the front of the list.
List<L> *List<L>::addtofront(L data) {
Node<L> *new_node = new Node<L>(data);
if(first == 0 && last == 0) {
first = new_node;
last = new_node;
} else {
// there will be no previous since
// it is the first
new_node->next = first;
first->prev = new_node;
first = new_node;
}
num_elements++;
}
template <class L>
// Append data to the end of the list.
List<L> *List<L>::addtoback(L data) {
[...]
}
template <class L>
// Return a pointer to a copy of the first item in the list.
Node<L> *List<L>::getfirst() {
[...]
}
template <class L>
// Return a list of all but the first element of the original.
List<L> List<L>::getrest() {
[...]
}
template <class L>
// Print out the data in each node separated by a space.
void List<L>::show() {
Node<L> *current_node = first;
while (current_node != NULL) {
cout << current_node->data << " ";
current_node = current_node->next;
}
cout << endl;
}
template <class L>
// Returns a reversed version of toReverse
List<L> List<L>::reverse(List toReverse) {
[...]
}
你需要把所有的Template实现放在头文件中。由于编译器不知道函数可能具有什么类型,因此无法编译cpp文件。所以把所有的东西从List.cpp移到List.h,它应该可以工作了
模板函数必须留在你将要使用它的同一个数据单元中。这是因为模板不直接声明类型,它们在使用时被实例化。
把模板实现放在你的头文件中。
你可以将"list.cpp"文件重命名为"list.hpp"文件,并将其包含在"list.h"中。也有人使用"list.inc"
如果你的链接行看起来像这样,效果会更好:
g++列表。o UseList。0 - 0 UseList
是这样吗?
List.h
:
template <class L>
class List
{
/* ... */
List *addtofront(L);
但在List.cpp
中:
template <class L>
// Appends data to the front of the list.
List<L> *List<L>::addtofront(L data) {
我看到了List*
和List<L> *
相关文章:
- 链接器错误:函数的多个定义
- 编译器错误:函数调用在常量表达式中必须有一个常量值
- 错误:函数声明符之后的预期函数体
- C 错误 - 函数不能超载
- 如何修复传递参数时调用错误函数的主函数?(C++)
- 错误:函数调用中有两个参数
- 错误:函数不是“类”的静态数据成员 - C++
- C ,G 编译错误函数
- CMake 解析错误函数缺少结尾")"。而是找到带有文本的未终止字符串")
- C++ 错误 函数 2 的多重定义
- 错误:函数调用中从int到int(*)[4]的转换无效
- C++14 自动扣除错误:函数返回一个数组
- 视觉C++错误:函数必须返回一个值
- 错误:函数声明中的两种或多种数据类型
- 错误:函数未在作用域中声明
- rtw_android.c错误:函数“strnicmp”的隐式声明[-Weror=隐式函数声明]
- 错误:函数中的return语句没有值,返回“void*”[-fpermission]
- 简单程序中的链接器错误:函数的多重定义
- 错误:函数参数太多
- 错误:函数__tmaincrtstartup中引用的未解析的外部符号_main