Assertion failed:in>>data 报错abort() has been called
本帖最后由 小易zjy 于 2021-12-26 14:41 编辑在进行数据结构课程设计时,所需排序的数据(ele类:其中Word存放单词信息,Num存放单词个数)包含多个信息(单词、单词个数),为了将待排序的数据按照单词个数进行排序,设计了存放待排序元素的数据表类datalist.
但是在进行文件读写时出现了Assertion failed:in>>data报错abort() has been called的错误,查阅CSDN上相关资料,可能是调用了野指针或是内存泄露,看得云里雾里也不知道该怎么解决呜呜呜{:5_99:}
报错信息在datalist.h line 79 小菜鸟一枚,麻烦各位大佬了(或者有没有更合适的数据结构可以实现包含多个数据类型的结构体按照某一关键字的排序呢)
datalist.h
#pragma once
#ifndef DATALIST_H
#define DATALIST_H
#include <iostream>
#include <cassert>
#include"ele.h"
using namespace std;
const int DefaultSize = 50;
template <typename E, typename K>class dataList {
public:
dataList(int sz = DefaultSize) {
maxSize = sz;
currentSize = 0;
Element = new E;
assert(Element);
}
dataList(dataList<E, K>& R) {
maxSize = R.maxSize;
currentSize = R.currentSize;
Element = new E;
assert(Element);
for (int i = 0; i < currentSize; i++) {
Element = R.Element;
}
}
virtual ~dataList() {
delete[]Element;
}
virtual int Length() {
return currentSize;
}
virtual bool SeqSearch(const K k1)const;
virtual void keySearch(const K k1)const;
virtual int keyCount(const K k1)const;
void newDatalist(const K k1, dataList<E, K>& R);
virtual void repeatSearch(const K k1)const;
virtual bool Insert(E& e1);
virtual bool Remove(K k1, E& e1);
char least();
void Swap(E& x, E& y) {
E temp = x;
x = y;
y = temp;
}
void Swap(int x, int y) {//#重载
E temp = Element;
Element = Element;
Element = temp;
}
E& display(int x)
{
return Element;
}
dataList<E, K>& operator = (dataList<E, K>& R) {
maxSize = R.maxSize;
currentSize = R.currentSize;
Element = new E;
assert(Element);
for (int i = 0; i < currentSize; i++) {
Element = R.Element;
}
return *this;
}
E& operator [] (int i) {
return Element;
}
int Partition(const int low, const int high);
friend ostream& operator << (ostream& out, dataList<E, K>& R) {
for (int i = 0; i < R.currentSize; i++) {
out << R.Element << endl;
}
return out;
}
friend istream& operator >> (istream& in, dataList<E, K>& R) {
E data;
while (!in.eof()) {
assert(in >> data);
R.Insert(data);
}
return in;
}
protected:
E* Element;
int maxSize, currentSize;
};
template <typename E, typename K >bool dataList<E, K>::Insert(E& e1) {
if (currentSize == maxSize) {
return false;
}
Element = e1;
currentSize++;
return true;
}
template <typename E, typename K>bool dataList<E, K>::Remove(K k1, E& e1) {
if (currentSize == 0) {
return false;
}
int i;
for (i = 0; i < currentSize && Element != k1; i++);
if (i == currentSize) {
return false;
}
e1 = Element;
Element = Element;
currentSize--;
return true;
}
template <typename E, typename K>bool dataList<E, K>::SeqSearch(const K k1)const {
int i;
for (i = 0; i < currentSize; i++) {
if (Element == k1)return true;
}
return false;
//return (i == currentSize) ? 0 : (i + 1);
}
template <typename E, typename K>void dataList<E, K>::keySearch(const K k1)const
{
for (int i = 0; i < currentSize; i++)
{
if (Element == k1) { cout << Element << "\t"; }
}
}
template <typename E, typename K>int dataList<E, K>::keyCount(const K k1)const
{
int num = 0;
for (int i = 0; i < currentSize; i++)
{
if (Element == k1) { num++; }
}
return num;
}
template <typename E, typename K> void dataList<E, K>::newDatalist(const K k1, dataList<E, K>& R)
{
for (int i = 0; i < currentSize; i++)
{
if (Element == k1)R.Insert(Element);
}
}
template <typename E, typename K>void dataList<E, K>::repeatSearch(const K k1)const
{
for (int i = 0; i < currentSize; i++)
{
if (Element == k1)cout << Element << "\t"; /*return Element;*/
}
//return (i == currentSize) ? 0 : (i + 1);
}
template <typename E, typename K>char dataList<E, K>::least()
{
char least = 'a';
int count = keyCount('a');
for (char x = 'b'; x <= 'z'; x++)
{
if ((keyCount(x) < count) && keyCount(x) != 0) { count = keyCount(x); least = x; }
}
return least;
//leastSearch(Initial, least);
//return (i == currentSize) ? 0 : (i + 1);
}
template <typename E, typename K>int dataList<E, K>::Partition(const int low, const int high) {
//数据表类的共有函数
int pivotpos = low;
E pivot = Element; //基准元素
for (int i = low + 1; i <= high; i++) {
//检测整个序列, 进行划分
if (Element < pivot) {
pivotpos++;
if (pivotpos != i) {
Swap(Element, Element);
}
}//小于基准的交换到左侧去
}
Element = Element;
Element = pivot; //将基准元素就位
return pivotpos; //返回基准元素位置
}
#endif
ele.h
#pragma once
#include <string>
#include <cassert>
#include <iomanip>
#include <iostream>
using namespace std;
class ele {
public:
int key;
ele(int num = 0, string word = " ") {
Num = num;
Word = word;
key = Num;
}
ele(ele& pr) {
Num = pr.Num;
Word = pr.Word;
key = Num;
}
int getKey() {
return key;
}
void setKey(int k1) {
key = k1;
}
int getNum() {
return Num;
}
void setNum(int num) {
Num = num;
}
string getWord() {
return Word;
}
void setWord(string word) {
Word = word;
}
ele& operator = (const ele& pr) {
Num = pr.Num;
Word = pr.Word;
key = Num;
return *this;
}
ele& operator = (int k1) {
Num = k1;
key = Num;
return *this;
}
friend bool operator < (const ele& pr, int k1) {
return (pr.Num < k1);
}
friend bool operator <= (const ele& pr, int k1) {
return (pr.Num <= k1);
}
friend bool operator > (const ele& pr, int k1) {
return (pr.Num > k1);
}
friend bool operator >= (const ele& pr, int k1) {
return (pr.Num >= k1);
}
friend bool operator == (const ele& pr, int k1) {
return (pr.Num == k1);
}
friend bool operator != (const ele& pr, int k1) {
return (pr.Num != k1);
}
friend bool operator < (int k1, const ele& pr) {
return (k1 < pr.Num);
}
friend bool operator <= (int k1, const ele& pr) {
return (k1 <= pr.Num);
}
friend bool operator > (int k1, const ele& pr) {
return (k1 > pr.Num);
}
friend bool operator >= (int k1, const ele& pr) {
return (k1 >= pr.Num);
}
friend bool operator == (int k1, const ele& pr) {
return (k1 == pr.Num);
}
friend bool operator != (int k1, const ele& pr) {
return (k1 != pr.Num);
}
friend bool operator < (const ele& pr1, const ele& pr2) {
return (pr1.Num < pr2.Num);
}
friend bool operator <= (const ele& pr1, const ele& pr2) {
return (pr1.Num < pr2.Num);
}
friend bool operator > (const ele& pr1, const ele& pr2) {
return (pr1.Num > pr2.Num);
}
friend bool operator >= (const ele& pr1, const ele& pr2) {
return (pr1.Num >= pr2.Num);
}
friend bool operator == (const ele& pr1, const ele& pr2) {
return (pr1.Num == pr2.Num);
}
friend bool operator != (const ele& pr1, const ele& pr2) {
return (pr1.Num != pr2.Num);
}
friend ostream& operator << (ostream& out, ele& pr) {
out << pr.Word << '\t';
return out;
}
friend istream& operator >> (istream& in, ele& pr) {
in >> pr.Num >> pr.Word;
pr.key = pr.Num;
return in;
}
private:
int Num;
string Word;
};
main.cpp
#include <sstream>
#include <fstream>
#include <cassert>
#include<iostream>
#include<string.h>
#include<algorithm>
#include"datalist.h"
#include"ele.h"
using namespace std;
int main
{
dataList<ele, int>list1;
remove("test.txt");
for (int j = 0; j < 10; j++)
{
ofstream ofs;
ofs.open("test.txt", ios::out | ios::app);
ofs << j << " " << "string" << endl;
ofs.close();
}
ifstream fin2;
fin2.open("test.txt");
assert(fin2 >> list1);
fin2.close();
cout << list1 << endl;
return 0;
} 我觉得datalist.h和ele.h写的都没有问题,应该就是文件读写的时候出错了
页:
[1]