Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数终止时的访问冲突——为什么会发生这种情况?_C++_Visual C++ - Fatal编程技术网

C++ 函数终止时的访问冲突——为什么会发生这种情况?

C++ 函数终止时的访问冲突——为什么会发生这种情况?,c++,visual-c++,C++,Visual C++,loadInventoryData函数一终止,我的程序就会以访问冲突错误终止: CIS2252 Programming Project.exe中0x0F9BCCC8(msvcp110d.dll)处未处理的异常:0xC0000005:访问冲突读取位置0x007DD314 调用堆栈: msvcp110d.dll!std::_Container_base12::_Orphan_all() Line 216 C++ CIS2252 Programming Project.exe!std::_Str

loadInventoryData函数一终止,我的程序就会以访问冲突错误终止:

CIS2252 Programming Project.exe中0x0F9BCCC8(msvcp110d.dll)处未处理的异常:0xC0000005:访问冲突读取位置0x007DD314

调用堆栈:

msvcp110d.dll!std::_Container_base12::_Orphan_all() Line 216    C++
CIS2252 Programming Project.exe!std::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >::_Free_proxy() Line 680  C++
CIS2252 Programming Project.exe!std::_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >::~_String_alloc<0,std::_String_base_types<char,std::allocator<char> > >() Line 656   C++
CIS2252 Programming Project.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> >() Line 965   C++
CIS2252 Programming Project.exe!loadInventoryData(std::vector<InventoryItem,std::allocator<InventoryItem> > & invMast) Line 220 C++
CIS2252 Programming Project.exe!main() Line 45  C++
CIS2252 Programming Project.exe!__tmainCRTStartup() Line 536    C
CIS2252 Programming Project.exe!mainCRTStartup() Line 377   C
msvcp110d.dll!STD::容器C++基础:12::
CIS2252编程项目.exe!STD::SyrgIGHYLO::FyFr+Pro()680行C++
CIS2252编程项目.exe!STD::StRyGangelOLC:~:~(656)C++
CIS2252编程项目.exe!STD::Basic字符串::~ Basic(965)C++
CIS2252编程项目.exe!Load SooLogyDATA(STD::vector和VimaST)220 C++
CIS2252编程项目.exe!第45行C++
CIS2252编程项目.exe__tmainCRTStartup()行536 C
CIS2252编程项目.exe!mainCRTStartup()行377 C
它终止的代码部分是:

void loadInventoryData(vector<InventoryItem> &invMast)
{
    // Loads invmast.dat or creates one if none exists
    fstream invFile;
    invFile.open("invmast.dat", ios::in | ios::binary);
    if (!invFile)
    {
        cerr << "WARNING: Unable to open file invmast.dat, no inventory records loaded!" << endl;
    }
    else
    {
        cout << "File invmast.dat opened successfully." << endl;

        string itemName;
        int itemQuantity;
        double itemPrice;

        while (invFile && !invFile.eof())
        {
            invFile.read(reinterpret_cast<char *>(&itemName), sizeof(itemName));
            invFile.read(reinterpret_cast<char *>(&itemQuantity), sizeof(itemQuantity));
            invFile.read(reinterpret_cast<char *>(&itemPrice), sizeof(itemPrice));
            invMast.emplace_back(itemName, itemQuantity, itemPrice); // Create element using constructor with data loaded into invLoader
        }

    }
}
void loadInventoryData(矢量和invMast)
{
//加载invmast.dat,如果不存在,则创建一个
fstream文件;
open(“invmast.dat”,ios::in | ios::binary);
如果(!invFile)
{

cerr您正在错误地使用
字符串。在这一行中:

invFile.read(reinterpret_cast<char *>(&itemName), sizeof(itemName));
方法
reserve()
为您分配一块要写入的内存,
c_str()
为您获取一个指向字符串的c风格指针,
capacity()
为您提供刚刚保留的内存大小


虽然不确定这是最佳解决方案,但您可能需要使用
字符
数组。

您使用的
字符串
不正确。在这一行中:

invFile.read(reinterpret_cast<char *>(&itemName), sizeof(itemName));
方法
reserve()
为您分配一块要写入的内存,
c_str()
为您获取一个指向字符串的c风格指针,
capacity()
为您提供刚刚保留的内存大小

虽然不确定这是否是最好的解决方案,但您可能需要一个
char
数组。

这行看起来不正确

invFile.read(reinterpret_cast<char *>(&itemName), sizeof(itemName));
invFile.read(reinterpret_cast(&itemName),sizeof(itemName));
以这种方式使用的sizeof(itemName)不会返回字符串的容量,堆栈跟踪也会抱怨字符串分配问题

这一行看起来是错误的

invFile.read(reinterpret_cast<char *>(&itemName), sizeof(itemName));
invFile.read(reinterpret_cast(&itemName),sizeof(itemName));

以这种方式使用的sizeof(itemName)不会返回字符串的容量,堆栈跟踪也会抱怨字符串分配问题

忘了提及,调用是在向量声明之后立即进行的:loadInventoryData(invMast)
对指向
std::string
的指针重新解释\u cast
在我看来非常可疑。尝试使用实际的
char*
缓冲区,然后将其复制到
std::string
中,看看这是否解决了问题?我怀疑您正在覆盖指向缓冲区或类似对象的
string
指针,然后它尝试释放您在析构函数中写入的垃圾数据。文件是字节流,而不是对象流。您对
重新解释\u cast
sizeof
的使用完全中断。您获得指向对象和对象大小的指针,然后假装这是将对象序列化为字节流所需的。不是。Serialization从一个字节流到一个字节流需要一个序列化和反序列化代码,而你还没有写过。@ DaviSdWurz我应该提到,这种二进制文件读写的方法是从一本书中得到的:C++如何编写程序第九版本,这是我们在课堂上所教的。所以我不知道正确的方法。我们将发布一个“正确”方法的示例,因为这将是一个有用的参考框架,而不是“您的代码不好”@正确的方法如下:1)设计一种文件格式,在字节级别定义它。2)编写代码,将平台使用的任何内部格式转换为该文件格式。3)编写代码,将平台使用的任何内部格式转换为平台使用的任何内部格式。(或在您喜爱的搜索引擎中输入“序列化”。)这种使用
reinterpret\u cast
的方式刚刚被打破。忘了提及,调用是在向量声明之后立即进行的:loadInventoryData(invMast)
对指向
std::string
的指针重新解释\u cast
在我看来非常可疑。尝试使用实际的
char*
缓冲区,然后将其复制到
std::string
中,看看这是否解决了问题?我怀疑您正在覆盖指向缓冲区或类似对象的
string
指针,然后它尝试释放您在析构函数中写入的垃圾数据。文件是字节流,而不是对象流。您对
重新解释\u cast
sizeof
的使用完全中断。您获得指向对象和对象大小的指针,然后假装这是将对象序列化为字节流所需的。不是。Serialization从一个字节流到一个字节流需要一个序列化和反序列化代码,而你还没有写过。@ DaviSdWurz我应该提到,这种二进制文件读写的方法是从一本书中得到的:C++如何编写程序第九版本,这是我们在课堂上所教的。所以我不知道正确的方法。我们将发布一个“正确”方法的示例,因为这将是一个有用的参考框架,而不是“您的代码不好”@正确的方法如下:1)设计一种文件格式,在字节级别定义它。2)编写代码,将平台使用的任何内部格式转换为该文件格式。3)编写代码,将平台使用的任何内部格式转换为平台使用的任何内部格式。(或在您喜爱的搜索引擎中输入“序列化”。)这种使用
reinterpret\u cast
的方式刚刚被打破。为什么不跳过所有这些并使用呢?@CaptainObvlious我同意,这可能是最好的解决方案,但我想