2016-12-09 8 views
-1

Я начинаю личный проект, включающий связанный список на C++, и я получаю обнаруженную стек, обнаружив ошибку, однако программа всегда проходит всю дорогу. Я не считаю, что ошибка связана с классом учетной записи, которую я создал, я провел базовое тестирование и, похоже, работает нормально. Скорее всего, ошибка связана со связанным списком.C++ обнаружение обнаруженной ошибки

Также я только начинаю писать код на C++, поэтому, если вы заметили какие-либо стили оформления, которые я мог бы улучшить, не стесняйтесь, дайте мне знать.

Пожалуйста, объясните, почему я получаю ошибку разбиения стека и как ее исправить.

Вот код:

main.cpp-

#include <iostream> 
#include <string> 
#include "../include/Account.h" 
#include "../include/LinkedList.h" 

using namespace std; 

int main() { 
    Account a("Sue Jones", 003, 0.01, 10000); 
    Account b("John Smith", 001, 0.01, 5783); 
    LinkedList l; 
    l.addNode(a); 
    l.addNode(b); 
    l.printList(); 
    l.deleteNode(a.getID()); 
    l.printList(); 
    l.deleteNode(b.getID()); 
    l.printList(); 
    return 0; 
} 

LinkedList.h-

#ifndef LINKEDLIST_H 
#define LINKEDLIST_H 

#include "Account.h" 

class LinkedList { 
    private: 
     typedef struct node { 
      Account data; 
      node *next; 
     }* nodePtr; 

     nodePtr head; 
     nodePtr curr; 
     nodePtr temp; 

    public: 
     LinkedList(); 
     void addNode(Account add); 
     void deleteNode(int delID); 
     void printList(); 
}; 

#endif 

LinkedList.cpp-

#include <iostream> 
#include <cstdlib> 
#include "../include/LinkedList.h" 

using namespace std; 

LinkedList::LinkedList() { 
    head = NULL; 
    curr = NULL; 
    temp = NULL; 
} 

void LinkedList::addNode(Account add) { 
    nodePtr n; 
    n->next = NULL; 
    n->data = add; 

    if(head != NULL) { 
     curr = head; 
     while(curr->next != NULL) { 
      curr = curr->next; 
     } 
     curr->next = n; 
    } 
    else { 
     head = n; 
     curr = n; 
    } 
} 

void LinkedList::deleteNode(int delID) { 
    temp = head; 
    curr = head; 
    while(curr != NULL && curr->data.getID() != delID) { 
     temp = curr; 
     curr = curr->next; 
    } 
    if(curr != NULL) { 
     if(curr == head) { 
      curr = curr->next; 
      temp->next = curr; 
      head = curr; 
     } 
     else { 
      curr = curr->next; 
      temp->next = curr; 
     } 
    } 
} 

void LinkedList::printList() { 
    curr = head; 
    cout << "The linked list:" << endl; 
    while(curr != NULL) { 
     cout << curr->data.toString(); 
     cout << " --> " << endl; 
     curr = curr->next; 
    } 
    cout << "NULL" << "\n" << endl; 
} 

Account.h-

#ifndef ACCOUNT_H 
#define ACCOUNT_H 

#include <string> 

/** 
* Account class for bankSimulator project. 
*/ 
class Account { 
    private: 
     std::string name; 
     int id; 
     double interestRate; 
     double value; 
    public: 
     Account(std::string name, int id, double interestRate, double value); 
     std::string getName(); 
     int getID(); 
     double getInterestRate(); 
     double getValue(); 
     void accumulateInterest(double years); 
     std::string toString(); 
}; 

#endif 

Account.cpp-

#include "../include/Account.h" 
#include <iostream> 
#include <string> 

using namespace std; 

/** 
* Constructor for an account object. 
* @param name: the name of the account holder 
* @param id: the id for the account 
* @param interestRate: the interest rate of the account as a decimal 
* @param value: the current value of the account 
*/ 
Account::Account(string name, int id, double interestRate, double value) 
    :name(name), id(id), interestRate(interestRate), value(value) { 

} 

/** 
* Getter for the account name. 
* @return: the name of the account holder 
*/ 
string Account::getName() { 
    return name; 
} 

/** 
* Getter for the account ID. 
* @return: the ID of the account 
*/ 
int Account::getID() { 
    return id; 
} 

/** 
* Getter for the interest rate. 
* @return: the interest rate of the account 
*/ 
double Account::getInterestRate() { 
    return interestRate; 
} 

/** 
* Getter for the value of the account. 
* @return: the value of the account 
*/ 
double Account::getValue() { 
    return value; 
} 

/** 
* Addes the interest for the number of years annually. 
* @param years: the number of years 
*/ 
void Account::accumulateInterest(double years) { 
    while(years > 0) { 
     if(years >= 1) { 
      value += value*interestRate; 
      years--; 
     } 
     else { 
      value += value*interestRate*years; 
      break; 
     } 
    } 
} 

/** 
* Creates a string representation of an Account. 
* @return: the string of the Account 
*/ 
string Account::toString() { 
    string output; 
    output.append("Name: "); 
    output.append(name); 
    output.append("\nID: "); 
    output.append(to_string(id)); 
    output.append("\nInterestRate: "); 
    output.append(to_string(interestRate)); 
    output.append("\nValue: "); 
    output.append(to_string(value)); 
    output.append("\n"); 
    return output; 
} 

Отредактировано:

Вот output-

./bin/main 
The linked list: 
Name: Sue Jones 
ID: 3 
InterestRate: 0.010000 
Value: 10000.000000 
--> 
Name: John Smith 
ID: 1 
InterestRate: 0.010000 
Value: 5783.000000 
--> 
NULL 

The linked list: 
Name: John Smith 
ID: 1 
InterestRate: 0.010000 
Value: 5783.000000 
--> 
NULL 

The linked list: 
NULL 

*** stack smashing detected ***: ./bin/main terminated 
Makefile:18: recipe for target 'run' failed 
make: *** [run] Aborted (core dumped) 
+0

Когда появляется сообщение об ошибке «stack smash» и что именно он говорит? –

+2

Вы не создаете структуру «нового» узла в любом месте - как ваш список растет со временем? Прямо сейчас, я вижу стек, основанный на 'nodePtr', но это исчезнет, ​​когда вы оставите функцию' addNode() '. –

+0

Поместите эту информацию в вопрос, а не как комментарий, пожалуйста :) –

ответ

1

Вы забыли выделить память для узла. Лучше

void LinkedList::addNode(Account add) { 
    nodePtr n = new node; /// allocate memory for the node 
    n->next = NULL; 
    n->data = add; 

    if(head != NULL) { 
     curr = head; 
     while(curr->next != NULL) { 
      curr = curr->next; 
     } 
     curr->next = n; 
    } 
    else { 
     head = n; 
     curr = n; 
    } 
} 

И тогда вы должны также удалить узел,

void LinkedList::deleteNode(int delID) { 
    temp = head; 
    curr = head; 
    while(curr != NULL && curr->data.getID() != delID) { 
     temp = curr; 
     curr = curr->next; 
    } 
    if(curr != NULL) { 
     if(curr == head) { 
      curr = curr->next; 
      temp->next = curr; 
      delete head;   /// delete node 
      head = curr; 
     } 
     else { 
      temp->next = curr->next; 
      delete curr;   /// delete node 
     } 
    } 
} 

Я считаю, что это упражнение в изучении базовых навыков программирования. В противном случае лучше использовать std::list.

+0

Спасибо! Мне пришлось изменить способ отправки новой учетной записи функции 'addNode()' в дополнение к созданию нового узла, от отправки значения до отправки указателя учетной записи. –

 Смежные вопросы

  • Нет связанных вопросов^_^