C++ 我得到这个错误;运行我的代码时,在0x7BE7B103(msvcp140d.dll)处引发异常

C++ 我得到这个错误;运行我的代码时,在0x7BE7B103(msvcp140d.dll)处引发异常,c++,arrays,class,pointers,C++,Arrays,Class,Pointers,我的代码有3个类user,book和userList用户列表是一个创建用户数组的类。我是一个初学者,所以任何帮助都将不胜感激。 我会列出文件名和他们的代码后 用户.h #pragma一次 #ifndef用户 #定义用户 #包括 #包括 #杂注警告(禁用:4716) 使用名称空间std; 类用户 { 私人: 字符串名; 智力年龄; int-id; 字符串密码; 字符串电子邮件; 公众: 静态整数计数; void setName(字符串名); 字符串getName(); 无效设置密码(字符串密码);

我的代码有3个类
user
book
userList
用户列表是一个创建用户数组的类。我是一个初学者,所以任何帮助都将不胜感激。 我会列出文件名和他们的代码后 用户.h

#pragma一次
#ifndef用户
#定义用户
#包括
#包括
#杂注警告(禁用:4716)
使用名称空间std;
类用户
{
私人:
字符串名;
智力年龄;
int-id;
字符串密码;
字符串电子邮件;
公众:
静态整数计数;
void setName(字符串名);
字符串getName();
无效设置密码(字符串密码);
字符串getPassword();
无效设置电子邮件(字符串电子邮件);
字符串getEmail();
无效设置(整数);
int getAge();
void setId();
int getId();
用户();
用户(字符串名称、整数、字符串电子邮件、字符串密码);
用户(const user&u);
布尔运算符==(常量用户&u);
friend ostream&operator(istream&input,user&u);
};
#恩迪夫
user.cpp

#include "user.h"

void user::setName(string Name)
{
    name = Name;
}

string user::getName()
{
    return name;
}

void user::setPassword(string Password)
{
    password = Password;
}

string user::getPassword()
{
    return password;
}

void user::setEmail(string Email)
{
    Email = email;
}

string user::getEmail()
{
    return email;
}

void user::setAge(int Age)
{
    age = Age;
}

int user::getAge()
{
    return age;
}

void user::setId()
{
    count++;
    id = count;
}

int user::getId()
{
    return id;
}

user::user()
{
    age = 0;
    name = password = email = "";
}

user::user(string Name, int Age, string Email, string Password)
{
    name = Name;
    password = Password;
    email = Email;
    age = Age;
}

user::user(const user& u)
{
    name = u.name;
    age = u.age;
    email = u.email;
    password = u.password;
    id = u.id;
}

bool user::operator==(const user& u)
{
    bool status;
    if (name == u.name && age == u.age && email == u.email && id == u.id)
    {
        status = true;
    }
    else
    {
        status = false;
    }
}

ostream& operator<<(ostream& output, const user& u)
{
    output << endl << "User name :" << u.name << endl << "User id :" << u.id << endl << "User age :" << u.age << endl << "User email :" << u.email << endl << "User password :" << u.password << endl;
}

istream& operator>>(istream& input, user& u)
{

    input >> u.name >> u.age >> u.email >> u.password;
}
#包括“user.h”
void user::setName(字符串名称)
{
名称=名称;
}
字符串用户::getName()
{
返回名称;
}
void user::setPassword(字符串密码)
{
密码=密码;
}
字符串用户::getPassword()
{
返回密码;
}
void user::setEmail(字符串电子邮件)
{
电子邮件=电子邮件;
}
字符串用户::getEmail()
{
回复邮件;
}
无效用户::设置年龄(整数年龄)
{
年龄=年龄;
}
int user::getAge()
{
回归年龄;
}
void user::setId()
{
计数++;
id=计数;
}
int user::getId()
{
返回id;
}
user::user()
{
年龄=0;
姓名=密码=电子邮件=”;
}
user::user(字符串名称、整数、字符串电子邮件、字符串密码)
{
名称=名称;
密码=密码;
电子邮件=电子邮件;
年龄=年龄;
}
用户::用户(const user&u)
{
name=u.name;
年龄=美国年龄;
电子邮件=美国电子邮件;
密码=u.password;
id=u.id;
}
bool user::operator==(const user&u)
{
布尔状态;
如果(name==u.name&&age==u.age&&email==u.email&&id==u.id)
{
状态=真;
}
其他的
{
状态=假;
}
}

ostream&operator您的代码有几个问题

用户列表中
析构函数:

删除用户

这是错误的
delete
格式。这应该是:

删除[]个用户

由于使用了
new[]
来分配数组,因此必须使用
delete[]


另一个必须解决的主要问题是
UserList
中的这个问题:


ostream&operator
user*用户-->
std::vector用户--没有必要让这件事变得更加困难。一旦进行了更改,您编写的许多代码都可以被删除,如果您的错误神奇地消失,这也就不足为奇了。谢谢,但这是一个赋值,我们必须使用数组。您的赋值是否也要求您编写完全不必要的代码,如用户定义的复制构造函数、赋值运算符、,以及
书籍
用户
类的析构函数?事实上,
UserList
类需要这些函数,但缺少这些函数。
UserList
类违反了。我要说的是
Book
User
类不需要编写任何用户定义的副本构造函数。如果您查看这些成员,它们都是可以安全复制的,因此编译器版本的复制构造函数比任何手工编写的都更可取。编译器的版本可以工作,没有错误,效率高,等等。另外,您是否注意到编译器关于从需要返回值的函数中不返回值的警告
user&UserList::searchUserName(字符串名)
--在这个函数中返回什么?编写和运行调用未定义行为的代码很危险。在您完成这些功能之前,您的代码无法正确运行(现在可能会出现这种情况,因为
main
是一个玩具程序,但是
main
中的一个简单更改揭示了您代码中的所有主要错误)。非常感谢您的修改,我做了这些修改,但无论您遇到相同的错误,我仍然会遇到相同的错误,这些修复是程序正常运行所必需的。另外,最初的错误可能是由答案中提到的其中一个错误引起的,而现在您的错误是由程序中的其他内容引起的。如果您查看所有应该返回值的函数,您就没有这样做。例如,
bool Book::operator=
应该返回一个
bool
,但不返回任何内容。我修复了除bool类型之外的所有返回类型,最后它成功了。非常感谢。
#include "user.h"

void user::setName(string Name)
{
    name = Name;
}

string user::getName()
{
    return name;
}

void user::setPassword(string Password)
{
    password = Password;
}

string user::getPassword()
{
    return password;
}

void user::setEmail(string Email)
{
    Email = email;
}

string user::getEmail()
{
    return email;
}

void user::setAge(int Age)
{
    age = Age;
}

int user::getAge()
{
    return age;
}

void user::setId()
{
    count++;
    id = count;
}

int user::getId()
{
    return id;
}

user::user()
{
    age = 0;
    name = password = email = "";
}

user::user(string Name, int Age, string Email, string Password)
{
    name = Name;
    password = Password;
    email = Email;
    age = Age;
}

user::user(const user& u)
{
    name = u.name;
    age = u.age;
    email = u.email;
    password = u.password;
    id = u.id;
}

bool user::operator==(const user& u)
{
    bool status;
    if (name == u.name && age == u.age && email == u.email && id == u.id)
    {
        status = true;
    }
    else
    {
        status = false;
    }
}

ostream& operator<<(ostream& output, const user& u)
{
    output << endl << "User name :" << u.name << endl << "User id :" << u.id << endl << "User age :" << u.age << endl << "User email :" << u.email << endl << "User password :" << u.password << endl;
}

istream& operator>>(istream& input, user& u)
{

    input >> u.name >> u.age >> u.email >> u.password;
}
#pragma once
#ifndef BOOK_H
#define BOOK_H
#include<cstring>
#include<iostream>
#include "user.h"
#pragma warning( disable : 4716 )
using namespace std;
class Book
{
private:
    string title;
    string isbn;
    int id;
    string category;
    double averageRating;
    user author;
public:
    static int count;
    void setTitle(string Title);
    string getTitle();
    void setISBN(string ISBN);
    string getISBN();
    void setId();
    int getId();
    void setCategory(string Category);
    string getCategory();
    void setRating(double Rating);
    double getRating();
    void setAuthor(user User);
    user getAuthor();
    Book();
    Book(const Book& b);
    bool operator==(const Book& b);
    friend ostream& operator<<(ostream& output, const Book& b);
    friend istream& operator>>(istream& input, Book& b);

};
#endif
#include "Book.h"

void Book::setTitle(string Title)
{
    title = Title;
}

string Book::getTitle()
{
    return title;
}

void Book::setISBN(string ISBN)
{
    isbn = ISBN;
}

string Book::getISBN()
{
    return isbn;
}

void Book::setId()
{
    count++;
    id = count;
}

int Book::getId()
{
    return id;
}

void Book::setCategory(string Category)
{
    category = Category;
}

string Book::getCategory()
{
    return category;
}

void Book::setRating(double Rating)
{
    averageRating = Rating;
}

double Book::getRating()
{
    return averageRating;
}

void Book::setAuthor(user User)
{
    author = User;
}

user Book::getAuthor()
{
    return author;
}

Book::Book()
{
    title = isbn = category = "";
    averageRating = 0;
}

Book::Book(const Book& b)
{
    title = b.title;
    isbn = b.isbn;
    category = b.category;
    author = b.author;
    id = b.id;
    averageRating = b.averageRating;
}

bool Book::operator==(const Book& b)
{
    bool status;
    if (title == b.title && isbn == b.isbn && category == b.category && id == b.id && author == b.author)
    {
        status = true;
    }
    else
    {
        status = false;
    }
}

ostream& operator<<(ostream& output, const Book& b)
{
    output << endl << "Book title :" << b.title << endl << "Book ISBN :" << b.isbn << endl << "Book ID :" << b.id << endl << "Book category :" << b.category << endl << "Average rating :" << b.averageRating << endl;
}

istream& operator>>(istream& input, Book& b)
{
    input >> b.title >> b.isbn >> b.category;
}
#pragma once
#include<cstring>
#include<iostream>
#include "user.h"
using namespace std;
class UserList
{
private:
    user* users;
    int capacity;
    int usersCount;
public:
    UserList(int Capacity);
    void addUser(user user);
    user& searchUserID(int id);
    user& searchUserName(string name);
    void deleteUser(int id);
    friend ostream& operator<<(ostream& output, const UserList& userList);
    ~UserList();
};
#include "UserList.h"

UserList::UserList(int Capacity)
{
    usersCount = 0;
    capacity = Capacity;
    user udef;
    users = new user[capacity];
    for (int i = 0; i < capacity; i++)
    {
        users[i] = udef;
    }
}

void UserList::addUser(user newuser)
{
    users[usersCount] = newuser;
    usersCount++;
}
user& UserList::searchUserName(string name)
{
    cout << "Enter the user's name" << endl;
    cin >> name;
    for (int i = 0; i < usersCount; i++)
    {
        if (name == users[i].getName())
        {
            cout << users[i];
        }
        else cout << "USER NOT FOUND !!!!" << endl;
    }
}
user& UserList::searchUserID(int id)
{
    cout << "Enter the user's ID" << endl;
    cin >> id;
    for (int i = 0; i < usersCount; i++)
    {
        if (id == users[i].getId())
        {
            cout << users[i];
        }
        else cout << "USER NOT FOUND !!!!" << endl;
    }
}



void UserList::deleteUser(int id)
{
    cout << "Enter the user's ID" << endl;
    cin >> id;
    int i;
    for (i = 0; i < usersCount; i++)
    {
        if (id == users[i].getId())
        {
            break;
        }
    }
    for (i; i < usersCount; i++)
    {
        users[i] = users[i + 1];
    }
    usersCount--;
}
ostream& operator<<(ostream& output, const UserList& userList)
{
    for (int i = 0; i < userList.usersCount; i++)
    {
        output << "---- USER " << i + 1 << "----" << userList.users[i];
    }
}
UserList::~UserList()
{
    delete users;
}
#include<cstring>
#include<iostream>
#include "user.h"
#include "Book.h"
#include "UserList.h"
#pragma warning( disable : 4716 )
using namespace std;
int user::count = 0;
int Book::count = 0;
int main()
{
    int size;
    cout << "enter the size of the list" << endl;
    cin >> size;
    UserList ul1(size);
    cout << ul1 << endl;
    return 0;
}