01/10/2018, 14:51
Lỗi khi code danh sách liên kết
Xin Chào mọi người!
Sau khi xem loạt bài "link list " của Anh Đạt trên Youtube.
Mình viết lại chương trình khá là đầy đủ nội ( các file *.h, *.cpp) . Tuy nhiên khi chạy nó có báo lỗi như hình đính kèm.
Xin nhờ ACE xem chỉ gíup mình sửa lỗi này với ạh!
sau đây là các files đính kèm:
//1.file. SinhVien.h
#pragma once
#include <string>
class SinhVien{
public:
SinhVien(int mssv, const std::string & name, SinhVien * const next = 0)
{
mssv_ =mssv;
name_ =name;
next_ =next;
}
~SinhVien()
{
next_ =0;
}
int Mssv() const
{
return mssv_;
}
void SetMssv(int mssv)
{
mssv_= mssv;
}
std::string Name() const
{
return name_;
}
void SetName(const std::string & name)
{
name_= name;
}
SinhVien * Next() const
{
return next_;
}
void SetNext(SinhVien * const other) {
next_=other;
}
SinhVien * Addnext(int mssv, const std::string & name,
SinhVien * const next=0)
{
SinhVien * newSV = new SinhVien(mssv, name, next);
this-> next_ =newSV;
return newSV;
}
private:
int mssv_;
std:: string name_;
SinhVien * next_;
};
//2.file. SinhVien.cpp
#include <iostream>
#include <string>
#include "SinhVien.h"
SinhVien::SinhVien( int mssv, const std::string & name, SinhVien * const next /*= 0*/){
/*:mssv_(mssv), name_(name), next_(next)// trong phan nay chua su dung const, viet nhu the nay chi de lam quen voi cach dinh nghia*/
mssv_ =mssv;
name_ =name;
next_ =next;
}
SinhVien::~SinhVien()
{
next_ =0;
}
int SinhVien::Mssv() const
{
return mssv_;
}
void SinhVien::SetMssv(int mssv)
{
mssv_=mssv;
}
std::string SinhVien:: Name() const
{
return name_; // need to be check
}
void SinhVien::SetName(const std::string & name)
{
name_=name;
}
SinhVien * SinhVien::Next() const
{
return next_;
}
void SinhVien::SetNext(SinhVien * const other)
{
next_=other;
}
SinhVien * SinhVien:: Addnext(int mssv, const std::string & name, SinhVien * const next/*=0*/)
{
SinhVien * newSV = new SinhVien(mssv, name, next);
this-> next_ =newSV; // It means, ban than SinhVien co cai next_ ( next_ la con cua this/SinhVien)
std:: cout << this->name_ << "+" << newSV-> Name() << std::endl;
return newSV;
}
// 3.file chuong trinh. link-list_chuanbi class SinhVien.cpp
#include <iostream>
#include <string>
#include "SinhVien.h"
namespace {
std::string CreatSinhVienName(const std::string & prefix, int number)
{
char * mssv =new char[5];
_itoa(number, mssv, 10);
std:: string name = prefix;
name.append(mssv);
delete []mssv;
return name;
}
void RemoveSinhVien(SinhVien *const previous, SinhVien * remove)
{
SinhVien * next =remove->Next();
previous-> SetNext(next);
delete remove;
}
void RemoveSinhVienByMssv(SinhVien * const from, const int mssv)
{
SinhVien * previous = from;
SinhVien * index =0;
for (index=from->Next(); index->Next() !=0; index=index->Next()){
if (index->Mssv() == mssv){
RemoveSinhVien(previous, index);
break;
}
previous=index;
}
}
void RemoveFirst(SinhVien*first)
{
delete first;
first = 0;
}
void RemoveLast(SinhVien*from)
{
SinhVien * last =from;
SinhVien * previousOfLast = last;
while(last->Next() !=0){
previousOfLast=last;
last=last->Next();
}
delete last;
previousOfLast->SetNext(0);
}
void InsertAfter(SinhVien * const index, SinhVien * const insert)
{
SinhVien * next = index->Next();
index->SetNext(insert);
insert->SetNext(next);
}
void InsertBefore(SinhVien*const first, SinhVien*const index,
SinhVien* const insert)
{
SinhVien * previous = first;
while(previous->Next()->Mssv() != index-> Mssv()){
previous = previous->Next();
}
previous->SetNext(insert);
insert->SetNext(index);
}
void ClearList(SinhVien*first)
{
SinhVien * index = first;
while (index->Next() !=0){
SinhVien * remove = index;
index = index->Next();
std::cout << "removing sv " << remove->Mssv() << std::endl;
delete remove;
}
std::cout << "removing sv " << index->Mssv() << std::endl;
delete index;
}
}
int main(){
SinhVien *first=new SinhVien(1,"sv 1",0);
SinhVien * currentsv = first;
for (int mssv=2;mssv<5;mssv++)
{
std::string name=CreatSinhVienName("sv ",mssv);
SinhVien * newsv = new SinhVien(mssv,name,0);
currentsv -> SetNext(newsv);
currentsv = newsv;
}
return 0;
}

Bài liên quan
Trong file header bạn chỉ khai báo method, các thuộc tính thôi, không implement body method.
Xin cám ơn bạn Lê Hoài rất nhiều!
Mình remove body của thuộc tính trong header file. thì chương trình chạy được rồi.