"未定义函数引用"错误

3

我一直在与一个令人沮丧的错误斗争,已经花费了相当长的时间。

在将数据类型从 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) {
      [...]
}
4个回答

2

你需要将所有的模板实现放在头文件中。由于编译器不知道函数可能具有的类型,因此无法编译cpp文件。所以将List.cpp中的所有内容移动到List.h中,就可以解决问题。


1

模板函数必须保留在您将使用它的相同数据单元中。

这是因为模板不直接声明类型,而是在使用时实例化。

将模板实现放在头文件中。

您可以将文件“list.cpp”重命名为名为“list.hpp”的文件,并将其包含在“list.h”中。有些人还使用“list.inc”的约定。


0

就是这个吗?

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> *


0

如果你的链接行看起来是这样的,它应该能更好地工作:

g++ List.o UseList.o -o UseList


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接