【发布时间】:2014-04-29 16:16:37
【问题描述】:
我正在做一个程序来表示“学生”表。它具有“ID”、“First Name”和“Last Name”属性。我做了一个二维数组并将 cout 语句全部放在检查...正确的信息存储在正确的单元格中,但我收到一个我不认识的错误。 .cpp 文件的代码是:
#include "Table1.h"
#include <sstream>
#include <iostream>
using namespace std;
Table1::Table1(){
RowPos = 1;
ColPos = 0;
for(int i=1; i<16; i++)
{
for(int j=0; j<3 ; j++)
Students[i][j] = "FakeNull"; // fill the array with Null value
}
}
string Table1::InsertStudent(string ID, string FN, string LN){
Students[0][0] = "StudentID";
Students[0][1] = "FirstName";
Students[0][2] = "LastName";
Students[RowPos][ColPos] = ID; //Assign ID to first column
ColPos++; //Move to next column
Students[RowPos][ColPos] = FN;
ColPos++;
Students[RowPos][ColPos] = LN;
ColPos++;
ColPos = 0;
RowPos++;
}
void Table1::Print(string Name){
if(Name == "students"){
for(int i=1; i<16; i++)
{
int j=0;
if(Students[i][j] == "FakeNull")
break;
else
cout<< "("<< Students[i][j]<< ",";
j++;
cout<< Students[i][j]<< ",";
j++;
cout<< Students[i][j]<< ")";
}
}
}
我的 Table1.h 是:
#include <iostream>
using namespace std;
#include <string>
#ifndef GRADE_HEADER
#define GRADE_HEADER
class Table1
private:
string Students[16][3];
string Grades[16][3];
int RowPos;
int ColPos;
public:
Table1();
string InsertStudent(string, string, string);
string InsertGrade(string, string, string, string);
void Print(string);
void Select(string, string, int);
void Select(string, string, string);
void Select(string, string, char);
void Join();
string Converter(int);
};
#endif
错误如下:
*** glibc detected *** ./a.out: double free or corruption (out): 0xbfbdeae0 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xeadee2]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0x9a951f]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSs4_Rep10_M_destroyERKSaIcE+0x1b) [0x99099b]
/usr/lib/i386-linux-gnu/libstdc++.so.6(+0x909dc)[0x9909dc]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZNSsD1Ev+0x2e)[0x990a4e]
./a.out[0x8049299]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xe514d3]
./a.out[0x8048a71]
======= Memory map: ========
003a0000-003ca000 r-xp 00000000 08:01 150255 /lib/i386-linux-gnu/libm-2.15.so
003ca000-003cb000 r--p 00029000 08:01 150255 /lib/i386-linux-gnu/libm-2.15.so
003cb000-003cc000 rw-p 0002a000 08:01 150255 /lib/i386-linux-gnu/libm-2.15.so
005a5000-005a6000 r-xp 00000000 00:00 0 [vdso]
007c5000-007e5000 r-xp 00000000 08:01 150250 /lib/i386-linux-gnu/ld-2.15.so
007e5000-007e6000 r--p 0001f000 08:01 150250 /lib/i386-linux-gnu/ld-2.15.so
007e6000-007e7000 rw-p 00020000 08:01 150250 /lib/i386-linux-gnu/ld-2.15.so
00900000-009d8000 r-xp 00000000 08:01 393409 /usr/lib/i386-linux gnu/libstdc++.so.6.0.16
009d8000-009d9000 ---p 000d8000 08:01 393409 /usr/lib/i386-linux gnu/libstdc++.so.6.0.16
009d9000-009dd000 r--p 000d8000 08:01 393409 /usr/lib/i386-linux gnu/libstdc++.so.6.0.16
009de000-009e5000 rw-p 00000000 00:00 0
00c13000-00c2f000 r-xp 00000000 08:01 132412 /lib/i386-linux-gnu/libgcc_s.so.1
00c2f000-00c30000 r--p 0001b000 08:01 132412 /lib/i386-linux-gnu/libgcc_s.so.1
00c30000-00c31000 rw-p 0001c000 08:01 132412 /lib/i386-linux-gnu/libgcc_s.so.1
00e38000-00fdc000 r-xp 00000000 08:01 150260 /lib/i386-linux-gnu/libc-2.15.so
00fdc000-00fde000 r--p 001a4000 08:01 150260 /lib/i386-linux-gnu/libc-2.15.so
00fde000-00fdf000 rw-p 001a6000 08:01 150260 /lib/i386-linux-gnu/libc-2.15.so
00fdf000-00fe2000 rw-p 00000000 00:00 0
08048000-0804a000 r-xp 00000000 00:19 52698567
0804a000-0804b000 r--p 00001000 00:19 52698567
0804b000-0804c000 rw-p 00002000 00:19 52698567
09df7000-09e18000 rw-p 00000000 00:00 0 [heap]
b77c9000-b77cc000 rw-p 00000000 00:00 0
b77de000-b77e3000 rw-p 00000000 00:00 0
bfbbe000-bfbdf000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)
【问题讨论】:
-
请包含 Table1.h(并清理您的格式)——目前我们无法看到您在哪里初始化 Student 数组(如果未初始化,您会得到一个像你得到的错误)
-
请提供类声明
-
你如何定义
Students -
为了便于指导,需要完整的声明、定义和构造函数细节。
Table1构造函数没有足够的信息来确定应该如何使用二维Students“数组”。例如,Students是整数到字符串键值对的 STL 映射容器吗? -
我在底部添加了 Table.h 部分。
标签: c++ debugging free glibc backtrace