我是oop的新手,这是我第一次做一个涉及双链表的代码。当我完成并尝试编译我的代码时,出现以下错误:
ld: fatal: symbol 'Character::MoveLeft(int)' is multiply-defined:
(file /var/tmp//ccOhlQhs.o type=FUNC; file /var/tmp//cco5zfsu.o type=FUNC);
ld: fatal: symbol 'Character::MoveRight(int)' is multiply-defined:
(file /var/tmp//ccOhlQhs.o type=FUNC; file /var/tmp//cco5zfsu.o type=FUNC);
ld: fatal: symbol 'Character::getLetter()' is multiply-defined:
(file /var/tmp//ccOhlQhs.o type=FUNC; file /var/tmp//cco5zfsu.o type=FUNC);
ld: fatal: symbol 'Character::Character()' is multiply-defined:
(file /var/tmp//ccOhlQhs.o type=FUNC; file /var/tmp//cco5zfsu.o type=FUNC);
ld: fatal: symbol 'Character::Character()' is multiply-defined:
(file /var/tmp//ccOhlQhs.o type=FUNC; file /var/tmp//cco5zfsu.o type=FUNC);
ld: fatal: symbol 'Character::setLetter(char)' is multiply-defined:
(file /var/tmp//ccOhlQhs.o type=FUNC; file /var/tmp//cco5zfsu.o type=FUNC);
ld: fatal: symbol 'Character::Delete()' is multiply-defined:
(file /var/tmp//ccOhlQhs.o type=FUNC; file /var/tmp//cco5zfsu.o type=FUNC);
ld: fatal: symbol 'Character::Insert(char)' is multiply-defined:
(file /var/tmp//ccOhlQhs.o type=FUNC; file /var/tmp//cco5zfsu.o type=FUNC);
ld: fatal: file processing errors. No output written to a.out
collect2: error: ld returned 1 exit status
以下是我的头文件和实现文件:
//lab3Character.h
1 #include <iostream>
2 #include <string>
3
4 using namespace std;
5
6 class Character{
7 private:
8
9 char _letter;
10 Character* head;
11
12 public:
13
14 Character* next;
15 Character* prev;
16
17 Character();
18 char getLetter();
19 void setLetter(char);
20 void MoveLeft(int);
21 void MoveRight(int);
22 void Insert(char);
23 void Delete();
24
25 };
26
//.cpp//
1 #include <iostream>
2 #include <string>
3 #include "lab3Character.h"
4
5 using namespace std;
6
7 Character::Character(): _letter(0){
8 next = NULL;
9 prev = NULL;
10 head = NULL;
11 }
12
13 char Character::getLetter(){ return _letter;}
14
15 void Character::setLetter(char cha){
16 _letter=cha;
17 return;
18 }
19
20 void Character::MoveLeft(int K)
21 {
22 int i;
23
24 for(i=1; i<=K; i++)
25 {
26 if(prev != NULL)
27 {
28 next = prev->next;
29 prev = prev->prev;
30 }
31 else
32 break;
33 }
34 return;
35 }
36
37 void Character::MoveRight(int K)
38 {
39 int i;
40
41 for(i=1; i<=K; i++)
42 {
43 if(next != NULL)
44 {
45 prev = next->prev;
46 next = next->next;
47 }
48
49 else
50 break;
51 }
52
53 return;
54 }
55
56 void Character::Insert(char cha)
57 {
58
59 if(head == NULL)
60 {
61 Character* newptr = new Character;
62 newptr->setLetter(cha);
63 head = newptr;
64 prev = newptr;
65
66 }
67 else
68 {
69 Character* newnode = new Character;
70 newnode->setLetter(cha);
71 newnode->next = prev->next;
72 prev->next->prev = newnode;
73 prev->next = newnode;
74 newnode->prev = prev;
75
76 if(newnode->prev == NULL)
77 head = newnode;
78
79 }
80
81 return;
82 }
83
84 void Character::Delete()
85 {
86 int i;
87
88 if(prev == NULL)
89 return;
90
91 else
92 {
93 prev->prev->next = prev->next;
94 prev = prev->prev;
95
96 for(i=1; prev != NULL; i++)
97 {
98 next = prev->next;
99 prev = prev->prev;
100 }
101
102 head = next->prev;
103
104 }
105
106 return;
107 }
108
109
110
很抱歉这段长代码非常感谢您的帮助!
答案 0 :(得分:0)
在您的标头文件中,添加
#pragma once
在第一行,或使用
#ifndef LIST_H
#define LIST_H
// Rest of your header file comes here
#endif
希望这会有所帮助:)