双链表c ++代码中的未定义引用错误

时间:2018-03-04 02:41:51

标签: c++ compiler-errors undefined-reference

刚刚开始使用双向链接列表进行编程工作。我是c ++的新手。我们不允许在dList.cpp文件中包含任何文件。我得到一个"未定义的参考"我不懂的班级错误。

main.cpp代码

#include <iostream>
using namespace std;
#include "dList.cpp"
#define SMALL 200
#define MAX 100000
#define ROUNDS 100
int main(){
 int i, x[MAX];
 char ch[MAX];
 for(i=0;i<SMALL;i++) {x[i] = 2*SMALL-i; ch[i] = 'a'+ (i%26);}
 dList A(x,ch,SMALL), B;
 A.out(10);
 node *tmp = A.search(2*SMALL-8);
 A.moveFront(tmp);
 A.out(10);
 A.moveBack(tmp);
 A.out(10);    
 A.find('b');
 A.sort();
 A.out(10);
 A.out(10,'b');
A.addBack(500,'d');
A.addFront(501,'z');
A.out(10);
A.out(10,'b');
B.addFront(1,'a');
B.addBack(2,'b');
B.out(2);
 for(int j=0; j<ROUNDS; j++){
 cout << endl << "round " << j << endl;
 for(i=0;i<MAX;i++) {x[i] = 2*MAX-i; ch[i] = 'a'+ (i%26);}
 dList A(x,ch,MAX);
 node *tmp = A.search(2*MAX-8);
 A.moveFront(tmp);
 A.moveBack(tmp);
 A.sort();
 A.out(10);
 A.out(10,'b');
 }
}

到目前为止我为dList.cpp文件编写的代码是:

struct node {
    int key;
    char type;
    struct node *next;
    struct node *prev;
};

class dList {
    public:
            dList();
            dList(int ints[], char chars[], int size);
            void addFront(int size, char type);
            void addBack(int size, char type);
            node *search(int key);
            void find(char type);
            void moveFront(node *search);
            void moveBack(node *search);
            void out(int key, char = 'f');
            void sort();
};

我收到一条非常长的错误消息。

$ make
g++ -c main.cpp
g++ -c dList.cpp
g++ main.o dList.o -o dList
main.o:main.cpp:(.text+0xa5): undefined reference to `dList::dList(int*, char*, int)'
main.o:main.cpp:(.text+0xa5): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `dList::dList(int*, char*, int)'
main.o:main.cpp:(.text+0xb1): undefined reference to `dList::dList()'
main.o:main.cpp:(.text+0xb1): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `dList::dList()'
main.o:main.cpp:(.text+0xc8): undefined reference to `dList::out(int, char)'
main.o:main.cpp:(.text+0xc8): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `dList::out(int, char)'
main.o:main.cpp:(.text+0xd9): undefined reference to `dList::search(int)'
main.o:main.cpp:(.text+0xd9): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `dList::search(int)'
main.o:main.cpp:(.text+0xf3): undefined reference to `dList::moveFront(node*)'
main.o:main.cpp:(.text+0xf3): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `dList::moveFront(node*)'
main.o:main.cpp:(.text+0x10a): undefined reference to `dList::out(int, char)'
main.o:main.cpp:(.text+0x10a): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `dList::out(int, char)'
main.o:main.cpp:(.text+0x11d): undefined reference to `dList::moveBack(node*)'
main.o:main.cpp:(.text+0x11d): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `dList::moveBack(node*)'
main.o:main.cpp:(.text+0x134): undefined reference to `dList::out(int, char)'
main.o:main.cpp:(.text+0x134): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `dList::out(int, char)'
main.o:main.cpp:(.text+0x145): undefined reference to `dList::find(char)'
main.o:main.cpp:(.text+0x145): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `dList::find(char)'
main.o:main.cpp:(.text+0x151): undefined reference to `dList::sort()'
main.o:main.cpp:(.text+0x151): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `dList::sort()'
main.o:main.cpp:(.text+0x168): undefined reference to `dList::out(int, char)'
main.o:main.cpp:(.text+0x168): additional relocation overflows omitted from the output
main.o:main.cpp:(.text+0x17f): undefined reference to `dList::out(int, char)'
main.o:main.cpp:(.text+0x196): undefined reference to `dList::addBack(int, char)'
main.o:main.cpp:(.text+0x1ad): undefined reference to `dList::addFront(int, char)'
main.o:main.cpp:(.text+0x1c4): undefined reference to `dList::out(int, char)'
main.o:main.cpp:(.text+0x1db): undefined reference to `dList::out(int, char)'
main.o:main.cpp:(.text+0x1f2): undefined reference to `dList::addFront(int, char)'
main.o:main.cpp:(.text+0x209): undefined reference to `dList::addBack(int, char)'
main.o:main.cpp:(.text+0x220): undefined reference to `dList::out(int, char)'
main.o:main.cpp:(.text+0x306): undefined reference to `dList::dList(int*, char*, int)'
main.o:main.cpp:(.text+0x317): undefined reference to `dList::search(int)'
main.o:main.cpp:(.text+0x331): undefined reference to `dList::moveFront(node*)'
main.o:main.cpp:(.text+0x344): undefined reference to `dList::moveBack(node*)'
main.o:main.cpp:(.text+0x350): undefined reference to `dList::sort()'
main.o:main.cpp:(.text+0x367): undefined reference to `dList::out(int, char)'
main.o:main.cpp:(.text+0x37e): undefined reference to `dList::out(int, char)'
collect2: error: ld returned 1 exit status
make: *** [makefile:4: dList] Error 1

0 个答案:

没有答案