所以我真的很沮喪,爲什麼會發生這種情況。我正在實現一個類似於std :: string的類,但這是使用鏈接列表而不是數組。我的重載運算符=由於某種奇怪的原因而不起作用。下面你可以看到,當我在方法內部打印指針時,字符串被複制到鏈接列表中,但是當我用此指針創建一個字符串對象時,控制檯會打印出無限垃圾。任何想法,我在這裏失蹤? (我只粘貼相關的代碼)鏈接列表overload overload =運行時錯誤
static int NumAllocations = 0;
struct ListNode {
char info;
ListNode *next;
ListNode() : info('0'), next(0) {}
ListNode (char c) : info (c), next(0) {}
};
class MyString {
private:
ListNode *head;
static void delstring (ListNode *l);
static int strlen (ListNode * head);
static ListNode* strcpy (ListNode *dest, ListNode *src);
public:
MyString::MyString() : head(0) {}
MyString::MyString (ListNode *l) : head(l) {}
MyString::MyString(const MyString & s) {
if (s.head == 0)
head = 0;
else
head = strcpy(head, s.head);
}
MyString MyString::operator = (const MyString & s){
ListNode *renew = NULL;
if (head != s.head) {
if (head != 0)
delstring(this -> head);
head = strcpy(head, s.head);
// printList(head); (this prints out the string just fine, so it must be the constructor ? but what about it ?!
MyString res (head);
return res;
}
}
MyString::~MyString(){
if (head == 0)
return;
ListNode *temp = NULL;
do {
temp = head -> next;
delete head;
-- NumAllocations;
head = temp;
} while (temp != 0);
}
STATIC公共職能
ListNode* MyString::strcpy (ListNode *dest, ListNode *src){
dest = new ListNode (src -> info);
++ NumAllocations;
ListNode *iter = dest;
for (ListNode *ptr = src -> next; ptr != 0; ptr = ptr ->next){
iter -> next = new ListNode (ptr -> info);
iter = iter -> next;
++ NumAllocations;
}
return dest;
}
void MyString::delstring (ListNode *l){
if (l == 0)
return;
ListNode *temp = NULL;
do {
temp = l -> next;
delete []l;
-- NumAllocations;
l = temp;
} while (temp != 0);
l = 0;
}
類定義將很好地協助這項工作。節點管理代碼更是如此。我看到零爲什麼'dest'是* *提供給名爲'strcpy'的成員。它是按值傳遞的,不管它是在第一行中立即*丟失,所以它可能是一個局部變量而不是參數。該功能看起來可能是一個問題,所以我會從那裏開始。 – WhozCraig
我添加了類定義。 strcpy是std :: string的成員函數,並且該類應儘可能相同,因此可以選擇名稱。就像我提到的那樣,列表被正確複製,因爲當我在返回前打印它時,它打印的很好,當我嘗試在operator = method的最後2行中創建一個具有該指針「頭部」的新對象時,它會混亂起來。 – Tangleman