RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 790863
Accepted
Johnny Rage
Johnny Rage
Asked:2020-02-27 14:55:30 +0000 UTC2020-02-27 14:55:30 +0000 UTC 2020-02-27 14:55:30 +0000 UTC

类方法中的内存访问错误

  • 772

问题如下:我在类中使用列表(禁止使用 STL)我至少编写了方法。此外,当我在 main 中描述实例并尝试调用 addElement 方法时,会发生内存访问错误。怎样成为?为什么会这样?

Main.cpp 列表

#include "stdafx.h"
#include "vset.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    vset a;
    a.AddElement(&a.ilast, 5);
    //  a.AddElement(&a.last, 7);
    a.PrintList(a.ihead);
    return 0;
}

列出vset.cpp类的实现

    #pragma once
    #include "vset.h"
    #include <iostream>
    using namespace std;

    vset::SingleList * vset::makeFirst(int d)
    {
        SingleList *cRec = new SingleList;
        cRec->Data = d; cRec->next = 0; cRec->prev = 0;
        return cRec;
    }

    void vset::AddElement(vset::SingleList **last, int d)
    {
        vset::SingleList *cRec = new SingleList;
        cRec->Data = d; cRec->next = 0; cRec->prev = *last;
        (*last)->next = cRec;
        *last = cRec;
    }

    vset::SingleList * vset::search(SingleList * const pbeg, int d)
    {
        SingleList *cRec = pbeg;
        while (cRec)
        {
            if (cRec->Data == d)break;
            cRec = cRec->next;
        }
        return cRec;
    }

    bool vset::remove(vset::SingleList **head, vset::SingleList **last, int key)
    {
        if (SingleList *pkey = search(*head, key))
        {
            if (pkey == *head)
            {
                *head = (*head)->next;
                (*head)->prev = 0;
            }
            else if (pkey == *last)
            {
                *last = (*last)->prev;
                (*last)->next = 0;
            }
            else {
                (pkey->prev)->next = pkey->next;
                (pkey->next)->prev = pkey->prev;
            }
            delete pkey;
            return true;
        }
        return false;
    }

    vset::SingleList * vset::insert(vset::SingleList * const head, vset::SingleList **last, int key, int d)
    {
        if (SingleList *pkey = search(head, key))
        {
            SingleList *cRec = new SingleList;
            cRec->Data = d; cRec->next = pkey->next;
            cRec->prev = pkey; pkey->next = cRec;
            if (pkey != *last) (cRec->next)->prev = cRec;
            else *last = cRec;
            return cRec;
        }
        return 0;
    }

    void vset::PrintList(vset::SingleList * const head)
    {
        SingleList *cRec = head;
        while (cRec)
        {
            cout << cRec->Data << " ";

            cRec = cRec->next;
        }
    }

    void getData()
    {

    }

    vset::vset()
    {
        SingleList *ihead = makeFirst(0);
        SingleList *ilast = ihead;
    //  getLast(&last);
    }

    vset::vset(int d)
    {
        SingleList *ihead = makeFirst(d);
        SingleList *ilast = ihead;
    }
    /*vset::SingleList getLast(vset::SingleList **last)
    {
        static vset::SingleList **olast;
        if (!last) olast = last;
        return **olast;
    }*/
    vset::~vset()
    {
    }

vset.h 接口

    #pragma once

    class vset
    {
    public:
        struct SingleList
        {
            int Data;

            SingleList *next;
            SingleList *prev;
        };
        SingleList *ihead;
        SingleList *ilast;
    public:
        vset();
        vset::vset(int d);
        ~vset();
        SingleList * makeFirst(int d);
        void AddElement(SingleList **last, int d);
        SingleList * search(SingleList * const pbeg, int d);
        bool remove(SingleList **head, SingleList **last, int key);
        SingleList * insert(SingleList * const head, SingleList **last, int key, int d);
        void PrintList(SingleList * const head);
        void getData();
        vset::SingleList getLast(vset::SingleList);
    };
c++
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    KoVadim
    2020-02-27T16:35:48Z2020-02-27T16:35:48Z

    让我们开始修复错误:)

    • #pragma once在cpp类?严重地?
    • vset::vset(int d);- 这就是您不需要在类声明中编写的方式。这样写是否正确,vset(int d);甚至这样写更好explicit vset(int d);
    • 构造函数-析构函数中的错误

      vset::vset()
      {
          SingleList *ihead = makeFirst(0);
          SingleList *ilast = ihead;
      //  getLast(&last);
      }
      

    此处声明了指针,其名称与类变量相交。从构造函数退出后,这些指针将丢失,并且类变量的值将保持未初始化状态。在这样的地方写构造函数是正确的

        vset::vset()
        {
            ihead = makeFirst(0);
            ilast = ihead;
        }
    

    在那之后,代码至少已经开始并且可以正常工作了。

    但是其中还有另一个泄漏-析构函数中没有释放内存。好处是您可以编写一个 clear 函数来清除内容并在析构函数中简单地调用它。

    但是还有一些注意事项。AddElement 函数需要一个指向最后一个元素的指针。如果你只传递一个指向任意元素的指针,那么它会破坏列表。(它会像一个叉子)。因此,最好从那里完全删除此参数。

    此外,您拥有的功能名称不一致。还有小写字母,还有CamelCase,还有camelCase。把所有东西都放在同一个视图上。

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5