尝试创建结构对象,在位置0x3FE00000中创建访问冲突写入错误
Attempt to create a structure object creating an Access Violation Writing Error In Location 0x3FE00000
我正在学习使用结构和制作基于文本的 RPG,我知道项目应该是我的代码中的一个类,但是,因为我使用的是结构并且我是 c++ 和编程的新手,我不明白我的代码到底出了什么问题也不知道如何修复它,错误发生在我尝试创建一个名为 player 的结构对象的地方。(我也想为我混乱的代码道歉,我正在乱搞,一旦我得到我的工作,就会清理它。
这是我的第一次尝试,我没有尝试太多,因为我不确定到底该尝试什么。
#include "pch.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct item {
double resistance, attack;
string name;
};
item none, WoodenBoots, LeatherChestplate, WoodenShield, WoodenClub, WoodenSword;
struct mobs {
int health, attack;
double droprate, resistance;
item drops[3];
};
item GetData(string object);
int main()
{
none.attack = 0;
none.resistance = 0;
none.name = "none";
WoodenBoots.resistance = .05;
WoodenBoots.attack = 0;
WoodenBoots.name = "Wooden Boots";
LeatherChestplate.resistance = .2;
LeatherChestplate.attack = 0;
LeatherChestplate.name = "Leather Chestplate";
WoodenShield.resistance = .1;
WoodenShield.attack = 0;
WoodenShield.name = "Wooden shield";
WoodenClub.resistance = 0;
WoodenClub.attack = 1.2;
WoodenClub.name = "Wooden Club";
WoodenSword.resistance = .05;
WoodenSword.attack = 1.5;
WoodenSword.name = "Wooden Sword";
mobs goblin;
goblin.attack = 1;
goblin.health = 10;
goblin.droprate = .5;
goblin.resistance = .1;
goblin.drops[1] = WoodenClub;
goblin.drops[2] = WoodenShield;
goblin.drops[3] = WoodenBoots;
mobs Alpha_Goblin;
Alpha_Goblin.attack = 2;
Alpha_Goblin.health = 15;
Alpha_Goblin.droprate = .5;
Alpha_Goblin.resistance = .1;
Alpha_Goblin.drops[1] = WoodenSword;
Alpha_Goblin.drops[2] = WoodenShield;
Alpha_Goblin.drops[3] = LeatherChestplate;
struct pdata {
item Playeritem[6];
item PlayerWeapon;
}player;
player.PlayerWeapon = WoodenSword;
cout << player.PlayerWeapon.name << endl;
player.PlayerWeapon = GetData("weapon");
player.Playeritem[0] = GetData("sheild");
player.Playeritem[1] = GetData("head");
player.Playeritem[2] = GetData("torso");
player.Playeritem[3] = GetData("legs");
player.Playeritem[4] = GetData("feet");
player.Playeritem[5] = GetData("hands");
string weapon = player.PlayerWeapon.name;
cout << weapon << endl;
system("pause");
}
我希望它创建结构并继续定义结构中的项目值。 我在结构 PDATA 的最后一行收到以下错误: 在结构.exe中0x57115139 (vcruntime140d.dll) 引发异常: 0xC0000005:访问违规写入位置0x3FE00000。
在C++数组从零到num_elements索引 - 1
所以如果你声明
int myArray[3];
,然后尝试访问:
myArray[3] = 5;
你将获得未定义的行为,这些行为可能表现为访问冲突,具体取决于编译器和调试设置。
在 Visual Studio 中,可以使用异常设置并在调试中运行时检查访问冲突异常,并在引发执行之前停止执行。
相关文章:
- 创建 2D 数组0xCCCCCCCC访问冲突写入位置
- 使用加速进程间创建消息队列 - 内存访问冲突
- 创建 Lua 状态时的访问冲突
- 为什么创建进程 API 调用会导致内存访问冲突?
- Windows错误:异常:使用从C++到Python的ctypes创建DLL时出现访问冲突或Windows错误193
- 挂钩创建文件抛出异常:读取访问冲突
- 创建 Vulkan 实例会导致访问冲突
- 尝试创建结构对象,在位置0x3FE00000中创建访问冲突写入错误
- 在访问冲突时创建转储文件
- 创建新数组时写入位置时发生访问冲突
- 创建链接列表时发生访问冲突错误
- 创建具有自动存储的自定义对象并推送回 std::vector 时访问冲突
- 我创建锁定文件的功能导致访问冲突
- 创建对象时发生SSE访问冲突崩溃
- 在 VB6 中创建的 "standard" DLL 在 Python 中调用时会出现访问冲突
- c++类:创建线程的对象+指向函数的指针=访问冲突
- 试图创建SFML窗口时发生访问冲突
- 创建指向指针列表的指针时发生访问冲突错误
- std::线程在使用参数创建时抛出访问冲突异常
- 在初始化列表中创建单例对象会导致访问冲突(仅释放模式)