gpt4 book ai didi

c++ - 使用类 - 分配大小无效

转载 作者:行者123 更新时间:2023-11-30 00:50:14 25 4
gpt4 key购买 nike

我有作业,但没用。我能猜到为什么它不起作用是 vs2013 的自动断点,它在输出框中显示它。

HEAP[hw2_ccc.exe]:分配大小无效 - 10(超过 fffdefff)

hw2_ccc.exe 中 0x000007FEFD9D940D 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x000000000012F350 处的 std::bad_alloc。

HEAP[hw2_ccc.exe]:分配大小无效 - 1(超过 fffdefff)

hw2_ccc.exe 中 0x000007FEFD9D940D 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x000000000012CE40 处的 std::bad_alloc。

hw2_ccc.exe 中 0x000007FEFD9D940D 处的第一次机会异常:Microsoft C++ 异常:[重新抛出] 在内存位置 0x0000000000000000。

hw2_ccc.exe 中 0x000007FEFD9D940D 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x000000000012CE40 处的 std::bad_alloc。

hw2_ccc.exe 中发生缓冲区溢出,损坏了程序的内部状态。

这是我的代码:

银行帐户.h

#ifndef H_bankAccount
#define H_bankAccount

#include <string>
#include <iostream>

using namespace std;

class bankAccount
{
public:
void setAccountName(string holderName);
void setAccountNumber(int accountNumber);
void setInterestRate(double interestRate);
void setAccountType(string accountType);
void depositBalance(double deposit);
void withdrawBalance(double withdraw);
void printAccountInformation();
void newAccount(string holderName, int accountNumber, string accountType, double interestRate);
string getAccountName();
bankAccount();
~bankAccount();

private:
string holderName;
int accountNumber;
string accountType;
double balance;
double interestRate;

};
#endif

银行账户.cpp

#include "bankAccount.h"

using namespace std;

bankAccount::bankAccount()
{

}

bankAccount::~bankAccount()
{

}

void bankAccount::newAccount(string holderName, int accountNumber, string accountType, double interestRate)
{
setAccountName(holderName);
setAccountNumber(accountNumber);
setAccountType(accountType);
setInterestRate(interestRate);
}

void bankAccount::setAccountName(string holderName)
{
bankAccount::holderName = holderName;
}

void bankAccount::setAccountNumber(int accountNumber)
{
bankAccount::accountNumber = accountNumber;
}

void bankAccount::setAccountType(string accountType)
{
bankAccount::accountType = accountType;
}

void bankAccount::setInterestRate(double interestRate)
{
bankAccount::interestRate = interestRate;
}

void bankAccount::depositBalance(double deposit)
{
bankAccount::balance += deposit;
}

void bankAccount::withdrawBalance(double withdraw)
{
bankAccount::balance -= withdraw;
}

void bankAccount::printAccountInformation()
{
cout << "Account Name: " << bankAccount::holderName << endl;
cout << "Account Type: " << bankAccount::accountType << endl;
cout << "Account Number: " << bankAccount::accountNumber << endl;
cout << "Account Interest Rate: " << bankAccount::interestRate << endl;
cout << "Account Balance :" << bankAccount::balance << endl;
}

string bankAccount::getAccountName()
{
return holderName;
}

主要.cpp

#include "bankAccount.h"

using namespace std;

int randAccountNum();
string randAccountType();
int interestRate(string accountType);
bool printAccount(bankAccount accounts[10]);

int main()
{
bankAccount account[10];
string accountNames[10] = { "Bob", "Jack", "Billy", "James", "Kathy", "John", "Jenny", "Penny", "Sue", "Louis" };
string accountType;
int accountNumber;
bool prAcc = true;

for (int i = 0; i < sizeof(account); i++)
{
accountType = randAccountType();
accountNumber = randAccountNum();
account[i].newAccount(accountNames[i], accountNumber, accountType, interestRate(accountType));
}

while (prAcc)
{
prAcc = printAccount(account);
}

system("pause");

return 0;
}

int randAccountNum()
{
int num = rand() % 1000 + 1;
return num;
}

string randAccountType()
{
string str;
int num = rand() % 2 + 1;
if (num = 1)
{
str = "Savings";
}
else {
str = "Checking";
}

return str;
}

int interestRate(string accountType)
{
int ir;

if (accountType == "Savings")
{
ir = 2;
}
else {
ir = 4;
}

return ir;
}

bool printAccount(bankAccount accounts[10])
{
string cont;
bool contL = true;
string accountName;

cout << "Enter account name: ";
cin >> accountName;
cout << endl;

for (int i = 0; i < sizeof(accounts); i++)
{
if (accounts[i].getAccountName() == accountName)
{
accounts[i].printAccountInformation();
}
}




while (contL)
{
cout << "Enter another name? (Yes/No): ";
cin >> cont;
if (cont == "Yes")
return true;
else if (cont == "No")
return false;
else
cout << "Invalid. Please enter Yes or No" << endl;
}


}

最佳答案

更改此声明

for (int i = 0; i < sizeof(account); i++)

for (int i = 0; i < sizeof(account) / sizeof(*account); i++)

功能也有错误

bool printAccount(bankAccount 账户[10]);

在这个声明中

for (int i = 0; i < sizeof(accounts); i++)

sizeof(accounts) 等于 sizeof( bankAccount * )

您必须为传递给函数的数组大小声明第二个参数。

考虑到您可以使用 std::array 类型的对象而不是数组。在这种情况下,数组的大小就不会有这样的问题。

关于c++ - 使用类 - 分配大小无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25650075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com