【问题标题】:I get this error"Exception thrown at 0x7BE7B103 (msvcp140d.dll) : when running my code [closed]我收到此错误“在 0x7BE7B103 (msvcp140d.dll) 处引发异常:运行我的代码时 [关闭]
【发布时间】:2021-04-01 08:09:43
【问题描述】:

我的代码有 3 个类 userbookuserList,用户列表是一个创建用户数组的类。我是一个初学者,所以任何帮助将不胜感激。 之后我将列出文件名及其代码 用户.h

#pragma once
#ifndef USER_H
#define USER_H
#include<cstring>
#include<iostream>
#pragma warning( disable : 4716 )
using namespace std;
class user
{
private:
    string name;
    int age;
    int id;
    string password;
    string email;

public:
    static int count;
    void setName(string Name);
    string getName();
    void setPassword(string Password);
    string getPassword();
    void setEmail(string Email);
    string getEmail();
    void setAge(int Age);
    int getAge();
    void setId();
    int getId();
    user();
    user(string Name, int Age, string Email, string Password);
    user(const user& u);
    bool operator ==(const user& u);
    friend ostream& operator<<(ostream& output, const user& u);
    friend istream& operator>>(istream& input, user& u);
};
#endif

用户.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;
}

书.h

#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

Book.cpp

#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;
}

用户列表.h

#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();
};

用户列表.cpp

#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;
}

main.cpp

#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;
}

【问题讨论】:

  • user* users; --> std::vector&lt;user&gt; users; -- 没有必要让它变得比它需要的更难。一旦进行了更改,您编写的许多代码都可以删除,如果您的错误神奇地消失了也就不足为奇了。
  • 谢谢,但这是一项任务,我们必须使用数组
  • 您的赋值是否还要求您编写完全不必要的代码,例如BookUser 类的用户定义的复制构造函数、赋值运算符和析构函数?事实上,正是 UserList 类需要这些功能,但缺少这些功能。 UserList 类违反了 rule of 3
  • 我的意思是BookUser 类不需要编写任何用户定义的复制构造函数。如果您查看成员,它们都是可安全复制的,因此编译器的复制构造函数版本比任何手写的都更可取。编译器的版本可以工作,没有错误,没有效率等。此外,您是否注意到编译器关于从需要返回值的函数返回不值的警告? user&amp; UserList::searchUserName(string name) -- 你在这个函数中返回什么?
  • 嗯,这是编写和运行调用未定义行为的代码的危险。在您完成这些功能之前,您的代码将无法正确运行(现在可能,因为 main 是一个玩具程序,但对 main 的简单更改会揭示您代码中的所有主要错误)。

标签: c++ arrays class pointers


【解决方案1】:

您的代码存在几个问题。

UserList析构函数中:

delete users;

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

delete [] users;

由于使用new[]分配数组,所以必须使用delete []


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

ostream&amp; operator&lt;&lt;(ostream&amp; output, const UserList&amp; userList)

您需要return output;,但您的代码无法这样做。不从具有返回值的函数返回值会调用未定义的行为

事实上,您的一些函数要求您返回值,但您没有这样做。因此,对这些函数的任何调用都会导致未定义的行为。

【讨论】:

  • 非常感谢您的修改,但我仍然遇到同样的错误
  • 无论您是否遇到相同的错误,您的程序都需要这些修复程序才能正常工作。另外,可能最初的错误是由答案中提到的错误之一引起的,现在您的错误是由程序中的其他问题引起的。
  • 如果您查看所有应该返回值的函数,您没有这样做。例如,bool Book::operator== 应该返回 bool 但不返回任何内容。
  • 我修复了除 bool 类型之外的所有返回类型,终于成功了,非常感谢。
猜你喜欢
  • 2022-07-15
  • 2012-05-17
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
  • 2020-10-03
  • 2021-03-01
  • 1970-01-01
相关资源
最近更新 更多