C++出现debug assertion failed 是什么原因

xn155308 2018-07-20 03:51:21
创建对象的时候就出现问题,单步调试也看不出来,谁可以帮帮


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip> // setw()右对齐函数
#include<string>
using namespace std;

class man //人类
{
protected:
char name[10];
string sex;
public:
man(char* s_name, string s_sex)
{
strcpy(name, s_name);
sex = s_sex;
}
char* getName() { return name; }
//string 获取性别() { return sex; }
//void 设置性别(char a) { sex = a; }
virtual void print()
{}
friend class school;
};
class workman :virtual public man //员工类
{
private:
int num;
public:
workman(char* s_name = "NONE", string s_sex = 0, int n = 0) :man(s_name, s_sex)
{
num = n;
}
int getnum() { return num; }
void print()
{
cout << setw(10) << name << setw(10) << sex;
cout << setw(15) << num << endl;
}
void print1()
{
cout << setw(10) << name << setw(10) << sex;
cout << setw(15) << num;
}
friend class school;
};

class student :virtual public man //学生类
{
private:
int id;
public:
student(char* s_name = "NONE", string s_sex = 0, int d = 0) :man(s_name, s_sex)
{
id = d;
}
int gateid() { return id; }
void print()
{
cout << setw(10) << name << setw(10) << sex;
cout << setw(15) << id << endl;
}
void print1()
{
cout << setw(10) << id << endl;
}
friend class school;
};
class zaizhixuesheng :public workman, public student
{
public:
zaizhixuesheng(char* s_name = "NONE", string s_sex = 0, int n = 0, int d = 0) :student(s_name, s_sex, d), workman(s_name, s_sex, n), man(s_name, s_sex)
{}
void print()
{
workman::print1();
student::print1();
}
friend class school;
};
class School
{
public:
workman w[10];
student s[10];
zaizhixuesheng z[10];
char name[10];
string sex;
int num;
int i;
int id;
void insert(int choice);
void Delete(int choice);
void show();
void search(int choice);
};

int str(char* s1, char* s2) //判断字符串内容是否相同
{
while (*s1 == *s2)
{
if (*s1 == '\0') return 1;
s1++;
s2++;
}
return 0;
}
void School::insert(int choice)
{
if (choice == 1)
{

cout << "请输入增加教职工信息" << endl;
cout << "请按格式输入:员工名称 员工性别 员工号" << endl;
cin >> name >> sex >> num;
for (i = 0; str(w[i].getName(), name) == 0 && i<10; i++) //str(yp[i].getName(),name)==0名称是否不一样
{
if (str(w[i].getName(), "NONE")) //str(yp[i].getName(),"NONE")名称不存在
{
w[i] = workman(name, sex, num);
break;
}
}
if (i == 10) { cout << "空间已满,无法存储,"; }
cout << setw(10) << " 名称" << setw(10) << "性别" << setw(15) << "编号" << endl;
w[i].print();
}
else if (choice == 2)
{

cout << "请输入增加学生信息" << endl;
cout << "请按格式输入:学生名称 学生性别 学生号" << endl;
cin >> name >> sex >> id;
s[i] = student(name, sex, id);
for (i = 0; str(s[i].getName(), name) == 0 && i<10; i++) //str(yp[i].getName(),name)==0名称是否不一样
if (str(s[i].getName(), "NONE")) //str(yp[i].getName(),"NONE")名称不存在
{

break;
}

if (i == 10) { cout << "空间已满,无法存储,"; }
cout << setw(10) << " 名称" << setw(10) << "性别" << setw(15) << "编号" << endl;
s[i].print();

}
else if (choice == 3)
{

cout << "请输入增加zaizhixuesheng信息" << endl;
cout << "请按格式输入:名称 性别 员工号 学生号" << endl;
cin >> name >> sex >> num >> id;
for (i = 0; str(z[i].getName(), name) == 0 && i<10; i++) //str(yp[i].getName(),name)==0名称是否不一样
if (str(z[i].getName(), "NONE")) //str(yp[i].getName(),"NONE")名称不存在
{
z[i] = zaizhixuesheng(name, sex, num, id);
break;
}

if (i == 10) { cout << "空间已满,无法存储,"; }
cout << setw(10) << " 名称" << setw(10) << "性别" << setw(15) << "员工号" << setw(10) << "学生号" << endl;
z[i].print();

}
}
void School::search(int choice)
{
if (choice == 1)
{
cout << "请输入查询教职工名字" << endl;
cin >> name;
for (i = 0; i<10; i++)
{
if (str(w[i].getName(), name) == 1)
{
cout << "员工:" << endl << setw(10) << "名称" << setw(10) << "性别" << setw(15) << "员工号" << endl << endl;
w[i].print();
break;
}
else
{
cout << "该人员不存在" << endl;
}
}
}
else if (choice == 2)
{
cout << "请输入查询学生名字" << endl;
cin >> name;
for (i = 0; i<10; i++)
{

if (str(s[i].getName(), name) == 1)
{
cout << "学生:" << endl << setw(10) << "名称" << setw(10) << "性别" << setw(15) << "员工号" << endl << endl;
s[i].print();
break;
}
else
{
cout << "该人员不存在" << endl;
}
}
}
else if (choice == 3)
{
cout << "请输入查询zaizhixuesheng名字" << endl;
cin >> name;
for (i = 0; i<10; i++)
{

if (str(z[i].getName(), name) == 1)
{
cout << "zaizhixuesheng:" << endl << setw(10) << "名称" << setw(10) << "性别" << setw(15) << "员工号" << setw(10) << "学生号" << endl << endl;
z[i].print();
break;
}
else
{
cout << "该人员不存在" << endl;
}
}
}
}
void School::Delete(int choice)
{

if (choice == 1)
{
cout << "请输入删除教职工名字" << endl;
cin >> name;
char j;
cout << "员工:" << endl << setw(10) << "名称" << setw(10) << "性别" << setw(15) << "员工号" << endl << endl;
for (i = 0; i<10; i++)
{
if (strcmp(name, w[i].getName()) == 0)
break;
}
w[i].print();
if (i<10)
{
cout << "是否确定删除 Y/N:" << endl;
cin >> j;
if (j == 'Y')
{
for (; i<10; i++)
{
w[i] = w[i + 1];
}
cout << "删除成功" << endl;
}
if (j == 'N')
{
cout << "放弃删除" << endl;
}
}
else
{
cout << name << "不存在" << endl;
}
}
else if (choice == 2)
{
cout << "请输入删除学生名字" << endl;
cin >> name;
char j;
cout << "学生:" << endl << setw(10) << "名称" << setw(10) << "性别" << setw(15) << "学生号" << endl << endl;
for (i = 0; i<10; i++)
{
if (strcmp(name, s[i].getName()) == 0)
break;
}
s[i].print();
if (i<10)
{
cout << "是否确定删除 Y/N:" << endl;
cin >> j;
if (j == 'Y')
{
for (; i<10; i++)
{
s[i] = s[i + 1];
}
cout << "删除成功" << endl;
}
if (j == 'N')
{
cout << "放弃删除" << endl;
}
}
else
{
cout << name << "不存在" << endl;
}
}
else if (choice == 3)
{
cout << "请输入删除zaizhixuesheng名字" << endl;
cin >> name;
char j;
cout << "zaizhixuesheng:" << endl << setw(10) << "名称" << setw(10) << "性别" << setw(15) << "员工号" << setw(10) << "学生号" << endl << endl;
for (i = 0; i<10; i++)
{
if (strcmp(name, z[i].getName()) == 0)
break;
}
z[i].print();
if (i<10)
{
cout << "是否确定删除 Y/N:" << endl;
cin >> j;
if (j == 'Y')
{
for (; i<10; i++)
{
z[i] = z[i + 1];
}
cout << "删除成功" << endl;
}
if (j == 'N')
{
cout << "放弃删除" << endl;
}
}
else
{
cout << name << "不存在" << endl;
}
}
}
void School::show()
{
for (i = 0; str(w[i].getName(), "NONE") == 0 && i<10; i++)
{
cout << setw(10) << " 名称" << setw(10) << "性别" << setw(15) << "职工号" << endl << endl;
w[i].print();
}
for (i = 0; str(s[i].getName(), "NONE") == 0 && i<10; i++)
{
cout << setw(10) << " 名称" << setw(10) << "性别" << setw(15) << "学生号" << endl << endl;
s[i].print();
}
for (i = 0; str(z[i].getName(), "NONE") == 0 && i<10; i++)
{
cout << setw(10) << " 名称" << setw(10) << "性别" << setw(15) << "职工号" << setw(10) << "学生号" << endl << endl;
z[i].print();
}

}



void menusecond(string a)
{
cout << " ******欢迎进入高校人员信息管理系统*****" << endl;
cout << " 此为选择菜单 " << endl;
cout << " --------------------------------------------------- " << endl;
cout << " --------------------------------------------------- " << endl;
cout << " | **** 请选择您进行操作的人员**** | " << endl;
cout << " | 1.教工信息 | " << endl;
cout << " | 2.学生信息 | " << endl;
cout << " | 3.zaizhixuesheng信息 | " << endl;
cout << " --------------------------------------------------- " << endl;
cout << " --------------------------------------------------- " << endl;
}

int main()
{
School p1;
int a, i, a1;
while (1)
{
cout << " ******欢迎进入高校人员信息管理系统*****" << endl;
cout << " 此为主菜单 " << endl;
cout << " --------------------------------------------------- " << endl;
cout << " --------------------------------------------------- " << endl;
cout << " | **** 请选择您进行操作的类**** | " << endl;
cout << " | 1.增加信息 2.查询信息 | " << endl;
cout << " | 3.删除信息 4.显示信息 | " << endl;
cout << " | | " << endl;
cout << " --------------------------------------------------- " << endl;
cout << " --------------------------------------------------- " << endl;
cout << "请输入您的选择:";
cin >> a;
switch (a)
{
case 5: return 1; //退出程序
case 1:

menusecond("增加");
cout << "\t \t \t";
cin >> a1;
switch (a1)
{
case 1:p1.insert(a1);
break;
case 2:p1.insert(a1);
break;
case 3:p1.insert(a1);
break;
case 4:
continue;
}
break;
case 2:
menusecond("查询");
cout << "\t \t \t";
cin >> a1;
switch (a1)
{
case 1:p1.search(a1);
break;
case 2:p1.search(a1);
break;
case 3:p1.search(a1);
break;
case 4:
continue;
}
break;
case 3:
menusecond("删除");
cout << "\t \t \t";
cin >> a1;
switch (a1)
{
case 1:p1.Delete(a1);
break;
case 2:p1.Delete(a1);
break;
case 3:p1.Delete(a1);
break;
case 4:
continue;
}
break;
case 4:p1.show();
}
}
}
...全文
514 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
轻箬笠 2018-07-20
  • 打赏
  • 举报
回复
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip> // setw()右对齐函数
#include<string>
using namespace std;

class man //人类
{
protected:
char name[10];
string sex;
public:
man(char* s_name, string s_sex)
{
strcpy(name, s_name);
sex = s_sex;
}
char* getName() { return name; }
//string 获取性别() { return sex; }
//void 设置性别(char a) { sex = a; }
virtual void print()
{}
friend class school;
};
class workman :virtual public man //员工类
{
private:
int num;
public:
workman(char* s_name = "NONE", string s_sex = "", int n = 0) :man(s_name, s_sex)
{
num = n;
}
int getnum() { return num; }
void print()
{
cout << setw(10) << name << setw(10) << sex;
cout << setw(15) << num << endl;
}
void print1()
{
cout << setw(10) << name << setw(10) << sex;
cout << setw(15) << num;
}
friend class school;
};

class student :virtual public man //学生类
{
private:
int id;
public:
student(char* s_name = "NONE", string s_sex = "", int d = 0) :man(s_name, s_sex)
{
id = d;
}
int gateid() { return id; }
void print()
{
cout << setw(10) << name << setw(10) << sex;
cout << setw(15) << id << endl;
}
void print1()
{
cout << setw(10) << id << endl;
}
friend class school;
};
class zaizhixuesheng :public workman, public student
{
public:
zaizhixuesheng(char* s_name = "NONE", string s_sex = "", int n = 0, int d = 0) :student(s_name, s_sex, d), workman(s_name, s_sex, n), man(s_name, s_sex)
{}
void print()
{
workman::print1();
student::print1();
}
friend class school;
};
class School
{
public:
workman w[10];
student s[10];
zaizhixuesheng z[10];
char name[10];
string sex;
int num;
int i;
int id;
void insert(int choice);
void Delete(int choice);
void show();
void search(int choice);
};

int str(char* s1, char* s2) //判断字符串内容是否相同
{
while (*s1 == *s2)
{
if (*s1 == '\0') return 1;
s1++;
s2++;
}
return 0;
}
void School::insert(int choice)
{
if (choice == 1)
{

cout << "请输入增加教职工信息" << endl;
cout << "请按格式输入:员工名称 员工性别 员工号" << endl;
cin >> name >> sex >> num;
for (i = 0; str(w[i].getName(), name) == 0 && i<10; i++) //str(yp[i].getName(),name)==0名称是否不一样
{
if (str(w[i].getName(), "NONE")) //str(yp[i].getName(),"NONE")名称不存在
{
w[i] = workman(name, sex, num);
break;
}
}
if (i == 10) { cout << "空间已满,无法存储,"; }
cout << setw(10) << " 名称" << setw(10) << "性别" << setw(15) << "编号" << endl;
w[i].print();
}
else if (choice == 2)
{

cout << "请输入增加学生信息" << endl;
cout << "请按格式输入:学生名称 学生性别 学生号" << endl;
cin >> name >> sex >> id;

for (i = 0; str(s[i].getName(), name) == 0 && i<10; i++) //str(yp[i].getName(),name)==0名称是否不一样
if (str(s[i].getName(), "NONE")) //str(yp[i].getName(),"NONE")名称不存在
{
s[i] = student(name, sex, id);
break;
}

if (i == 10) { cout << "空间已满,无法存储,"; }
cout << setw(10) << " 名称" << setw(10) << "性别" << setw(15) << "编号" << endl;
s[i].print();

}
else if (choice == 3)
{

cout << "请输入增加zaizhixuesheng信息" << endl;
cout << "请按格式输入:名称 性别 员工号 学生号" << endl;
cin >> name >> sex >> num >> id;
for (i = 0; str(z[i].getName(), name) == 0 && i<10; i++) //str(yp[i].getName(),name)==0名称是否不一样
if (str(z[i].getName(), "NONE")) //str(yp[i].getName(),"NONE")名称不存在
{
z[i] = zaizhixuesheng(name, sex, num, id);
break;
}

if (i == 10) { cout << "空间已满,无法存储,"; }
cout << setw(10) << " 名称" << setw(10) << "性别" << setw(15) << "员工号" << setw(10) << "学生号" << endl;
z[i].print();

}
}
void School::search(int choice)
{
if (choice == 1)
{
cout << "请输入查询教职工名字" << endl;
cin >> name;
for (i = 0; i<10; i++)
{
if (str(w[i].getName(), name) == 1)
{
cout << "员工:" << endl << setw(10) << "名称" << setw(10) << "性别" << setw(15) << "员工号" << endl << endl;
w[i].print();
break;
}
else
{
cout << "该人员不存在" << endl;
}
}
}
else if (choice == 2)
{
cout << "请输入查询学生名字" << endl;
cin >> name;
for (i = 0; i<10; i++)
{

if (str(s[i].getName(), name) == 1)
{
cout << "学生:" << endl << setw(10) << "名称" << setw(10) << "性别" << setw(15) << "员工号" << endl << endl;
s[i].print();
break;
}
else
{
cout << "该人员不存在" << endl;
}
}
}
else if (choice == 3)
{
cout << "请输入查询zaizhixuesheng名字" << endl;
cin >> name;
for (i = 0; i<10; i++)
{

if (str(z[i].getName(), name) == 1)
{
cout << "zaizhixuesheng:" << endl << setw(10) << "名称" << setw(10) << "性别" << setw(15) << "员工号" << setw(10) << "学生号" << endl << endl;
z[i].print();
break;
}
else
{
cout << "该人员不存在" << endl;
}
}
}
}
void School::Delete(int choice)
{

if (choice == 1)
{
cout << "请输入删除教职工名字" << endl;
cin >> name;
char j;
cout << "员工:" << endl << setw(10) << "名称" << setw(10) << "性别" << setw(15) << "员工号" << endl << endl;
for (i = 0; i<10; i++)
{
if (strcmp(name, w[i].getName()) == 0)
break;
}
//w[i].print();//可能越界
if (i<10)
{
cout << "是否确定删除 Y/N:" << endl;
cin >> j;
if (j == 'Y')
{
for (; i<10; i++)
{
w[i] = w[i + 1];
}
cout << "删除成功" << endl;
}
if (j == 'N')
{
cout << "放弃删除" << endl;
}
}
else
{
cout << name << "不存在" << endl;
}
}
else if (choice == 2)
{
cout << "请输入删除学生名字" << endl;
cin >> name;
char j;
cout << "学生:" << endl << setw(10) << "名称" << setw(10) << "性别" << setw(15) << "学生号" << endl << endl;
for (i = 0; i<10; i++)
{
if (strcmp(name, s[i].getName()) == 0)
break;
}
s[i].print();
if (i<10)
{
cout << "是否确定删除 Y/N:" << endl;
cin >> j;
if (j == 'Y')
{
for (; i<10; i++)
{
s[i] = s[i + 1];
}
cout << "删除成功" << endl;
}
if (j == 'N')
{
cout << "放弃删除" << endl;
}
}
else
{
cout << name << "不存在" << endl;
}
}
else if (choice == 3)
{
cout << "请输入删除zaizhixuesheng名字" << endl;
cin >> name;
char j;
cout << "zaizhixuesheng:" << endl << setw(10) << "名称" << setw(10) << "性别" << setw(15) << "员工号" << setw(10) << "学生号" << endl << endl;
for (i = 0; i<10; i++)
{
if (strcmp(name, z[i].getName()) == 0)
break;
}
if (i < 10)
z[i].print();
if (i<10)
{
cout << "是否确定删除 Y/N:" << endl;
cin >> j;
if (j == 'Y')
{
for (; i<10; i++)
{
z[i] = z[i + 1];
}
cout << "删除成功" << endl;
}
if (j == 'N')
{
cout << "放弃删除" << endl;
}
}
else
{
cout << name << "不存在" << endl;
}
}
}
void School::show()
{
for (i = 0; str(w[i].getName(), "NONE") == 0 && i<10; i++)
{
cout << setw(10) << " 名称" << setw(10) << "性别" << setw(15) << "职工号" << endl << endl;
w[i].print();
}
for (i = 0; str(s[i].getName(), "NONE") == 0 && i<10; i++)
{
cout << setw(10) << " 名称" << setw(10) << "性别" << setw(15) << "学生号" << endl << endl;
s[i].print();
}
for (i = 0; str(z[i].getName(), "NONE") == 0 && i<10; i++)
{
cout << setw(10) << " 名称" << setw(10) << "性别" << setw(15) << "职工号" << setw(10) << "学生号" << endl << endl;
z[i].print();
}

}



void menusecond(string a)
{
cout << " ******欢迎进入高校人员信息管理系统*****" << endl;
cout << " 此为选择菜单 " << endl;
cout << " --------------------------------------------------- " << endl;
cout << " --------------------------------------------------- " << endl;
cout << " | **** 请选择您进行操作的人员**** | " << endl;
cout << " | 1.教工信息 | " << endl;
cout << " | 2.学生信息 | " << endl;
cout << " | 3.zaizhixuesheng信息 | " << endl;
cout << " --------------------------------------------------- " << endl;
cout << " --------------------------------------------------- " << endl;
}

int main()
{
School p1;
int a, i, a1;
while (1)
{
cout << " ******欢迎进入高校人员信息管理系统*****" << endl;
cout << " 此为主菜单 " << endl;
cout << " --------------------------------------------------- " << endl;
cout << " --------------------------------------------------- " << endl;
cout << " | **** 请选择您进行操作的类**** | " << endl;
cout << " | 1.增加信息 2.查询信息 | " << endl;
cout << " | 3.删除信息 4.显示信息 | " << endl;
cout << " | | " << endl;
cout << " --------------------------------------------------- " << endl;
cout << " --------------------------------------------------- " << endl;
cout << "请输入您的选择:";
cin >> a;
switch (a)
{
case 5: return 1; //退出程序
case 1:

menusecond("增加");
cout << "\t \t \t";
cin >> a1;
switch (a1)
{
case 1:p1.insert(a1);
break;
case 2:p1.insert(a1);
break;
case 3:p1.insert(a1);
break;
case 4:
continue;
}
break;
case 2:
menusecond("查询");
cout << "\t \t \t";
cin >> a1;
switch (a1)
{
case 1:p1.search(a1);
break;
case 2:p1.search(a1);
break;
case 3:p1.search(a1);
break;
case 4:
continue;
}
break;
case 3:
menusecond("删除");
cout << "\t \t \t";
cin >> a1;
switch (a1)
{
case 1:p1.Delete(a1);
break;
case 2:p1.Delete(a1);
break;
case 3:p1.Delete(a1);
break;
case 4:
continue;
}
break;
case 4:p1.show();
}
}
return 0;
}


问题有点多,标了几个,lz自己仔细检查下吧。debug的问题应该是string sex=0。string类型的数据不好赋值为0,应该赋值为空字符串
百合杰 2018-07-20
  • 打赏
  • 举报
回复
哪一行?
关于龙书第13章地形绘制的terrain项目运行出错问题 (注:龙书即:《DirectX9.0 3D游戏开发编程基础》) 在学习该教材时,当我们试着编译并运行13章地形绘制的terrain项目时,发现运行出错,并弹出了一个出错提示窗口,提示我们::访问vector 越界了 提示窗口的内容如下: -------------------------------------------------------------------- Microsoft Visual C++ Debug Library Debug Assertion failed! Program:...cuments and Settings\Administrator\Terrain\Debug\Terrain.exe File: d:\microsoft visual studio 10.0\vc\include\vector Line:932 Expression : vector subscript out of range For information on how your program can cause an assertion Failure , see the Visual C++ documentation on asserts. (Press Retry to debug the application) [终止(A)] [重试(R)] [忽略(I)] ---------------------------------------------------------------------------------- 问题主要出现在terrain.cpp 文件中的几个函数内部传递参数最终作为了vector的索引值,得到的索引值没有经过限定,导致超出了vector的界限-----最终访问越界. 修改的地方在下面这几处: 在terrain.cpp文档里搜索”//修改过!!!” bool Terrain::genTexture(D3DXVECTOR3* directionToLight) float Terrain::computeShade(int cellRow, int cellCol, D3DXVECTOR3* directionToLight) float Terrain::getHeight(float x, float z) 在terrain.cpp文档里搜索”//注意这里!!!” /int Terrain::getHeightmapEntry(int row, int col) bool Terrain::lightTerrain(D3DXVECTOR3* directionToLight) 在terrainDriver.cpp文档里搜索”//注意这里!!!” float height = TheTerrain->getHeight( pos.x, pos.z ); 我的修改主要是通过在传递索引值的地方,限定索引值的范围,这样就避免了vector访问越界,这也是龙书作者在写该13章的代码时一时所忽略掉的. 只修改过terrain.cpp文件中的内容,其他的都没动.所以可以只将terrain.cpp拷贝到工程中替换掉原来的就可以了.

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧