访问冲突c++ visual studio类
Access Violation C++ visual studio Classes
我得到一个:"在ConsoleApplication18.exe中的0x00E8316C抛出异常:0xC0000005:访问违规写入位置0xCDCDCDD1。"当我运行我的代码时,这就是全部,错误在我用注释标记的地方中断。感谢您的宝贵时间!
#include "iostream"
using namespace std;
class Transaction
{
public:
int USERID;
int Amount;
virtual void Report() //modify for each transaction type
{
}
virtual void Perform() //modify for each transaction type
{
}
};
class Transfer : public Transaction
{
public:
int transID; // id of the user to tranfer with
int transType; // type of transition; to or fro
void Perform()
{
cout << "n" << "Transfer" << "nn";
cout << "Enter the id of the user to tranfer with: ";
cin >> transID;
cout << "n" << "Transfer type:" << "n" << "1: To 2: Fro" << "n";
cin >> transType;
if (transType == 2)
{
int temp = transID;
transID = USERID;
USERID = temp;
}
cout << "n" << "Enter the amount you wish to withdraw: ";
cin >> Amount;
}
void Report()
{
cout << "nn" << "Transfer of " << Amount << "$ from user: #" << USERID << " to the user: #" << transID;
}
};
class Withdraw : public Transaction
{
public:
void Perform()
{
cout << "n" << "Withdraw" << "nn";
cout << "Enter the amount you wish to withdraw: ";
cin >> Amount;
}
void Report()
{
cout << "nn" << "Withdrawl of: " << Amount << "$";
}
};
class Deposit : public Transaction
{
public:
void Perform()
{
cout << "n" << "Deposit" << "n";
cout << "Enter the amount you wish to deposit: ";
cin >> Amount;
}
void Report()
{
cout << "nn" << "Deposit of: " << Amount << "$";
}
};
class User
{
public:
int ID;
int numTrans;
Transaction* t[100];
int TYPE;
User()
{
numTrans = 0;
}
void AddWithdraw()
{
t[numTrans] = new Withdraw;
numTrans++;
}
void AddDeposit()
{
t[numTrans] = new Deposit;
numTrans++;
}
void AddTransfer()
{
t[numTrans] = new Transfer;
numTrans++;
}
};
void main()
{
User*u[100];
int id;
int type;
int counter;
counter = 0;
for (int i = 0; i <= 100; i++)
{
u[i] = new User;
}
while (true)
{
type = 0;
cout << "nPlease enter your ID: ";
cin >> id;
cout << "n" << "Which transaction would you like to perform" << "n" << "1: Withdraw" << "n" << "2: Deposit" << "n" << "3: Transfer" << "n";
cin >> type;
if (type != 1 && type != 2 && type != 3)
{
type = 1;
}
for (int i = 0; i <= counter; i++)
{
if (u[i]->ID == id || i == counter)
{
if (i == counter)// Add the type of transaction to the transaction array
{
if (type == 1) { u[i]->AddWithdraw(); }
if (type == 2) { u[i]->AddDeposit(); }
if (type == 3) { u[i]->AddTransfer(); }
}
u[i]->t[u[i]->numTrans]->USERID = id; //BREAKS HERE
u[i]->t[u[i]->numTrans]->Perform();
for (int b = 0; b <= u[i]->numTrans; b++)
{
u[i]->t[b]->Report();
}
break;
}
}
// Report all of the previous transactions
counter++;
}
}
你的代码中有很多错误。
-
Transaction
缺少虚析构函数 -
你在填充数组时没有做任何边界检查
-
你的循环使用
<=
时,他们需要使用<
代替 -
访问
u[i]->t[]
数组时主循环越界 -
你没有分配每个分配的
User
的ID
字段,所以你不能通过他们的id找到用户。 -
您正在泄漏内存,因为您从未释放任何
Transaction
或User
对象。
试试这样写:
#include <iostream>
using namespace std;
class Transaction
{
public:
int USERID;
int Amount;
virtual Transaction() {}
virtual void Report() = 0;
virtual void Perform() = 0;
};
class Transfer : public Transaction
{
public:
int transID; // id of the user to tranfer with
int transType; // type of transition; to or fro
void Perform()
{
cout << "n" << "Transfer" << "nn";
cout << "Enter the id of the user to tranfer with: ";
cin >> transID;
cout << "n" << "Transfer type:" << "n" << "1: To 2: Fro" << "n";
cin >> transType;
if (transType == 2)
{
int temp = transID;
transID = USERID;
USERID = temp;
}
cout << "n" << "Enter the amount you wish to withdraw: ";
cin >> Amount;
}
void Report()
{
cout << "nn" << "Transfer of " << Amount << "$ from user: #" << USERID << " to the user: #" << transID;
}
};
class Withdraw : public Transaction
{
public:
void Perform()
{
cout << "n" << "Withdraw" << "nn";
cout << "Enter the amount you wish to withdraw: ";
cin >> Amount;
}
void Report()
{
cout << "nn" << "Withdrawl of: " << Amount << "$";
}
};
class Deposit : public Transaction
{
public:
void Perform()
{
cout << "n" << "Deposit" << "n";
cout << "Enter the amount you wish to deposit: ";
cin >> Amount;
}
void Report()
{
cout << "nn" << "Deposit of: " << Amount << "$";
}
};
class User
{
public:
int ID;
int numTrans;
Transaction* trans[100];
int TYPE;
User()
{
numTrans = 0;
}
~User()
{
for (int i = 0; i < numTrans; ++i)
delete trans[i];
}
Transaction* AddTransaction(int type)
{
if (numTrans == 100)
return NULL;
Transaction *t;
switch (type)
{
case 1:
t = new Withdraw;
break;
case 2:
t = new Deposit;
break;
case 3:
t = new Transfer;
break;
default:
return NULL;
}
trans[numTrans] = t;
numTrans++;
return t;
}
Transaction* AddWithdraw()
{
return AddTransaction(1);
}
Transaction* AddDeposit()
{
return AddTransaction(2);
}
Transaction *AddTransfer()
{
return AddTransaction(3);
}
};
void main()
{
User* users[100];
int numUsers = 0;
int id;
int type;
for (int i = 0; i < 100; ++i)
{
users[i] = new User;
users[i]->ID = i+1;
++numUsers;
}
while (true)
{
cout << "nPlease enter your ID: ";
cin >> id;
User *user = NULL;
for (int i = 0; i < numUsers; ++i)
{
if (users[i]->ID == id)
{
user = users[i];
break;
}
}
if (!user)
{
cout << "Invalid id!" << "n";
continue;
}
type = 0;
do
{
cout << "n" << "Which transaction would you like to perform" << "n" << "1: Withdraw" << "n" << "2: Deposit" << "n" << "3: Transfer" << "n" << "4: Quit" << "n";
cin >> type;
if ((type >= 1) && (type <= 4))
break;
cout << "Invalid selection!" << "n";
}
while (true);
if (type == 4)
break;
// Add the type of transaction to the transaction array
Transaction *trans = user->AddTransaction(type);
if (!trans)
{
cout << "Too many transactions for this id!" << "n";
continue;
}
trans->USERID = id;
trans->Perform();
for (int b = 0; b < user->numTrans; ++b)
user->trans[b]->Report();
}
for (int i = 0; i < numUsers; ++i)
delete users[i];
return 0;
}
相关文章:
- Visual Studio 2015:Extern "C" 和 "export" 关键字
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 试图在visual studio上用C++创建一个桌面应用程序
- 如何在MS Visual Studio 2019中运行QT UI
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- Visual Studio Code - C++ Debugger 無法啟動
- Visual Studio 2019:插入多个C++风格的单行注释
- Visual Studio Code "undefined reference to `WinMain@16'"
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- Visual Studio中的函数声明和函数定义问题
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- Visual Studio mkl_link_tool.exe链接错误
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 不同的Visual Studio版本中缺少.dll
- 用Visual Studio在C++中嵌入Julia
- Visual Studio 2017循环自动向量化问题
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- Visual Studio在尝试读取resource.txt文件时崩溃
- Visual Studio 2017 不允许我创建 C++ 专用模板