Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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++初学者,一直在尝试自己解决一些问题。我应该为一个函数编写代码,该函数接受指向某个对象的指针向量,遍历列表,并从列表中删除无效日期。 到目前为止,在测试时,我一直收到一个错误,上面写着:“异常抛出:读取访问冲突”,在过去的几天里,我一直试图找到这个错误,但没有结果_C++_Class_Pointers_Vector - Fatal编程技术网

C++;:如何纠正代码中的读取访问冲突 我是C++初学者,一直在尝试自己解决一些问题。我应该为一个函数编写代码,该函数接受指向某个对象的指针向量,遍历列表,并从列表中删除无效日期。 到目前为止,在测试时,我一直收到一个错误,上面写着:“异常抛出:读取访问冲突”,在过去的几天里,我一直试图找到这个错误,但没有结果

C++;:如何纠正代码中的读取访问冲突 我是C++初学者,一直在尝试自己解决一些问题。我应该为一个函数编写代码,该函数接受指向某个对象的指针向量,遍历列表,并从列表中删除无效日期。 到目前为止,在测试时,我一直收到一个错误,上面写着:“异常抛出:读取访问冲突”,在过去的几天里,我一直试图找到这个错误,但没有结果,c++,class,pointers,vector,C++,Class,Pointers,Vector,这是我的密码: #include <iostream> #include <string> #include <sstream> #include <vector> using namespace std; class Reminder { private: int m_day; int m_month; string m_description; bool value; public: Reminde

这是我的密码:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

class Reminder
{
private:
    int m_day;
    int m_month;
    string m_description;
    bool value;

public:

    Reminder(int day, int month, string description) : m_day(day), m_month(month), m_description(description)
    {

    }

    string toString()
    {
        stringstream sin;
        sin << m_month << "/" << m_day << ": " << m_description<<endl;
        return sin.str();
    }

    int getDay()
    {
        int day = m_day;
        return day;
    }

    int getMonth()
    {
        int month = m_month;
        return month;
    }



};

vector<Reminder*> removeInvalidDate(vector<Reminder*>& list)
{
    for (int count = 0; count < list.size(); count++)
    {
        if ((list[count]->getDay() < 1) || (list[count]->getDay() > 31))
            delete list[count];
        if ((list[count]->getMonth() < 1) || (list[count]->getMonth() > 12))
            delete list[count];
        else
            ;
    }

    return list;
}

int main()
{
    Reminder reminder1(7, 16, "Final Exam Due");
    Reminder reminder2(7, 4, "Independence Day Holiday");
    Reminder reminder3(1, 1, "Start of the New Year");
    Reminder reminder4(7, 30, "My Birthday");
    Reminder reminder5(1, -9, "Impossible Day1");
    Reminder reminder6(0, 0, "Impossible Day0");
    Reminder reminder7(0, 35, "Impossible day3");
    Reminder reminder8(13, 0, "Impossible");

    Reminder* pRe;
    pRe = new Reminder(reminder1);

    vector<Reminder*> list;

    list.push_back(pRe);

    pRe = new Reminder(reminder2);
    list.push_back(pRe);

    pRe = new Reminder(reminder3);
    list.push_back(pRe);

    pRe = new Reminder(reminder4);
    list.push_back(pRe);

    pRe = new Reminder(reminder5);
    list.push_back(pRe);

    pRe = new Reminder(reminder6);
    list.push_back(pRe);

    pRe = new Reminder(reminder7);
    list.push_back(pRe);

    pRe = new Reminder(reminder8);
    list.push_back(pRe);

    removeInvalidDate(list);

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
上课提醒
{
私人:
国际货币日;
国际货币单位月;
字符串m_描述;
布尔值;
公众:
提醒(整数天、整数月、字符串描述):m_天(天)、m_月(月)、m_描述(描述)
{
}
字符串toString()
{
罪恶;

sin如果日期和月份都无效,则删除列表[count]两次

if ((list[count]->getDay() < 1) || (list[count]->getDay() > 31))
    delete list[count];
if ((list[count]->getMonth() < 1) || (list[count]->getMonth() > 12))
    delete list[count];
if((list[count]->getDay()<1)| |(list[count]->getDay()>31))
删除列表[计数];
if((list[count]->getMonth()<1)| |(list[count]->getMonth()>12))
删除列表[计数];

如果日期和月份都无效,则删除列表[计数]两次

if ((list[count]->getDay() < 1) || (list[count]->getDay() > 31))
    delete list[count];
if ((list[count]->getMonth() < 1) || (list[count]->getMonth() > 12))
    delete list[count];
if((list[count]->getDay()<1)| |(list[count]->getDay()>31))
删除列表[计数];
if((list[count]->getMonth()<1)| |(list[count]->getMonth()>12))
删除列表[计数];
我认为问题要么在于设置和传递向量,要么在于一个类构造函数

好吧,你可以检验这两个假设。你真的应该这样做

首先,您可以通过将代码简化为使用
vector
,来测试构造函数是否导致了问题。只需将整数值设为日期,暂时不考虑月份部分

第二,你可以用最简单的代码设置最小向量,只需要一个元素,或者零个元素,然后再添加元素,直到问题再次出现

这类工作,测试您对代码的理解,是完全正常的,也是一个良好的开始习惯

这两种假设都不能解决你目前的问题,但我鼓励你回去做

此代码

    if ((list[count]->getDay() < 1) || (list[count]->getDay() > 31))
        delete list[count];
    if ((list[count]->getMonth() < 1) || (list[count]->getMonth() > 12))
        delete list[count];
    else
        ;
<>但是C++没有“代码”> ILIF <代码>。如果你不想删除同一个元素两次,它应该是类似于

的。
    if ((list[count]->getDay() < 1) || (list[count]->getDay() > 31))
    {
        delete list[count];
        continue;
    }
    if ((list[count]->getMonth() < 1) || (list[count]->getMonth() > 12))
    {
        delete list[count];
        continue;
    }
if((list[count]->getDay()<1)| |(list[count]->getDay()>31))
{
删除列表[计数];
继续;
}
if((list[count]->getMonth()<1)| |(list[count]->getMonth()>12))
{
删除列表[计数];
继续;
}
(或者
if(){…}或者if(){…}
或者其他什么)

<>第二个更严重的问题是,<代码>删除列表[Cudio] < /C>不是你如何从C++中的任何容器中删除元素。它大致上是如何在Python中完成的。C++中,在<代码>删除列表[计数] < /C> >之后,元素<代码>列表[计数]
仍然存在,但它是指向您刚刚删除的存储的悬空指针,并且可能永远不会被合法地取消引用。因为接下来要做的就是取消引用它(用于月检查),这会导致您的读取访问冲突

任何一本好书都会告诉你如何从向量中删除元素,你应该更愿意这样做,并且你应该首先避免在容器中存储(拥有)原始指针

我认为问题要么在于设置和传递向量,要么在于一个类构造函数

好吧,你可以检验这两个假设。你真的应该这样做

首先,您可以通过将代码简化为使用
vector
,来测试构造函数是否导致了问题。只需将整数值设为日期,暂时不考虑月份部分

第二,你可以用最简单的代码设置最小向量,只需要一个元素,或者零个元素,然后再添加元素,直到问题再次出现

这类工作,测试您对代码的理解,是完全正常的,也是一个良好的开始习惯

这两种假设都不能解决你目前的问题,但我鼓励你回去做

此代码

    if ((list[count]->getDay() < 1) || (list[count]->getDay() > 31))
        delete list[count];
    if ((list[count]->getMonth() < 1) || (list[count]->getMonth() > 12))
        delete list[count];
    else
        ;
<>但是C++没有“代码”> ILIF <代码>。如果你不想删除同一个元素两次,它应该是类似于

的。
    if ((list[count]->getDay() < 1) || (list[count]->getDay() > 31))
    {
        delete list[count];
        continue;
    }
    if ((list[count]->getMonth() < 1) || (list[count]->getMonth() > 12))
    {
        delete list[count];
        continue;
    }
if((list[count]->getDay()<1)| |(list[count]->getDay()>31))
{
删除列表[计数];
继续;
}
if((list[count]->getMonth()<1)| |(list[count]->getMonth()>12))
{
删除列表[计数];
继续;
}
(或者
if(){…}或者if(){…}
或者其他什么)

<>第二个更严重的问题是,<代码>删除列表[Cudio] < /C>不是你如何从C++中的任何容器中删除元素。它大致上是如何在Python中完成的。C++中,在<代码>删除列表[计数] < /C> >之后,元素<代码>列表[计数]仍然存在,但它是指向您刚刚删除的存储的悬空指针,并且可能永远不会被合法地取消引用。因为接下来要做的就是取消引用它(用于月检查),这会导致您的读取访问冲突


任何一本好书都会告诉你如何从向量中删除元素,你应该更喜欢这样做,并且你应该首先避免在容器中存储(拥有)原始指针。

vector
有点傻,因为你不需要任何堆分配,只要使用
vector
删除列表[count];
释放分配,但不从向量中移除指针。
removeInvalidDate()不必要地返回向量的副本。程序中不需要任何指针使用或手动内存分配。我是C++初学者,一直试图自己解决一些问题。C++也是一种非常复杂的语言,如果不是M的话。