Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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++_Oop_Destructor - Fatal编程技术网

C++ 我认为我的析构函数导致了一个错误的发生。你能帮我找出原因吗?

C++ 我认为我的析构函数导致了一个错误的发生。你能帮我找出原因吗?,c++,oop,destructor,C++,Oop,Destructor,问题在于我的赋值运算符,我忘记了在string类中为指针释放内存。我一定是不小心把它当成了复制构造函数;这是一个很好的教训,说明了为什么内存管理非常重要。谢谢大家的帮助。 我实现了我自己的string类,这似乎是调用堆栈中断之前的最后一个函数 String::~String(){ delete [] rep; len=0; } 有人能帮我理解问题是什么吗 下面是调用它的函数 template <class T> void SList<T>::Remov

问题在于我的赋值运算符,我忘记了在string类中为指针释放内存。我一定是不小心把它当成了复制构造函数;这是一个很好的教训,说明了为什么内存管理非常重要。谢谢大家的帮助。

我实现了我自己的string类,这似乎是调用堆栈中断之前的最后一个函数

String::~String(){
    delete [] rep;
    len=0;
}
有人能帮我理解问题是什么吗

下面是调用它的函数

template <class T> 
void SList<T>::RemoveAfter(typename SList<T>::Iterator i){        
    assert(i.nodePointer !=0 && i.nodePointer->next!=0);
    Node *save = i.nodePointer -> next;
    i.nodePointer->next = i.nodePointer->next->next;
    delete save;

}
错误:

Windows已在Algorithms.exe中触发断点。 这可能是由于堆损坏,这表明Algorithms.exe或它加载的任何DLL中存在错误。 这也可能是由于用户在Algorithms.exe具有焦点时按下F12。 输出窗口可能有更多诊断信息

中断的示例功能:

    String item1("Example"), item2("Example");
    SList<String> list1;        
    list1.AddFirst(item2);
    list1.AddFirst(item1);
    list1.AddLast("List Class");
    list1.AddLast("Functionality");

    SList<String>::Iterator i1;

    i1 = list1.Begin();     
    i1++;
    i1++;
    list1.RemoveAfter(i1);
String item1(“示例”)、item2(“示例”);
slistlist1;
清单1.AddFirst(第2项);
清单1.AddFirst(第1项);
列表1.AddLast(“列表类”);
列表1.AddLast(“功能”);
SList::迭代器i1;
i1=list1.Begin();
i1++;
i1++;
清单1.删除后(i1);
有效的例子

    SList<int> list1;       
    list1.AddFirst(1);
    list1.AddFirst(2);
    list1.AddLast(3);
    list1.AddLast(4);

    SList<int>::Iterator i1;

    i1 = list1.Begin();     
    i1++;
    i1++;
    list1.RemoveAfter(i1);


    system("pause");
slistlist1;
列表1.AddFirst(1);
列表1.AddFirst(2);
列表1.AddLast(3);
列表1.AddLast(4);
SList::迭代器i1;
i1=list1.Begin();
i1++;
i1++;
清单1.删除后(i1);
系统(“暂停”);
更多信息:

//Default Constructor
String::String(){
rep = new char[1];
rep[0] = '\0';
len = 0;
}  

//Constructor - Converts char* to String object
String::String(const char *s){
len=0;
const char *temp = s;
while(*temp){
    ++len;
    ++temp;
}//Sets len of rep to the length of s
rep = new char[len + 1];
for(int i=0; i<=len; ++i)
    rep[i]=s[i];
}

//Copy Constructor
String::String(const String &obj){
len=0;
char *temp = obj.rep;

while (*temp){
    ++len;
    ++temp;
}//Sets len of rep to length of obj.rep
rep = new char[len + 1];
for (int i = 0; i<=len; ++i)
    rep[i] = obj.rep[i];
}

//Assignment operator
const String& String::operator=(const String &rhs){
if (this != &rhs){
    len=0;
    char *temp = rhs.rep;
    while(*temp){
        ++len;
        ++temp;
    }//Sets len of this to length of rhs.rep
    for(int i = 0; i<=len;++i)
        rep[i]=rhs.rep[i];
}
return *this;
}
//默认构造函数
String::String(){
rep=新字符[1];
代表[0]='\0';
len=0;
}  
//构造函数-将字符*转换为字符串对象
字符串::字符串(常量字符*s){
len=0;
常量字符*temp=s;
while(*temp){
++len;
++温度;
}//将rep的len设置为s的长度
rep=新字符[len+1];

对于(int i=0;i,在赋值运算符中,您可能正在
char*rep
指向的分配区域边界之外写入,这可能会损坏堆栈(即导致未定义的行为)

当您稍后尝试解除分配这段内存时,损坏的堆栈可能会导致出现死机,这很有可能是运行应用程序时收到错误消息的原因

如消息本身所述,“这可能是由于堆的损坏造成的”


如何解决我的问题? 在赋值操作符中,您需要做三件事:

  • 取消分配此中以前分配的内存(除非this->len和rhs.len具有相同的值,如果是;也跳过步骤2)
  • 为rhs.rep分配与rhs相同的内存量(即rhs.len字节)
  • 更新此->len以表示新内容的长度
  • 将存储在rhs.rep指向的内存中的字节复制到this->rep指向的内存段

  • 目前,您只需执行步骤3和4。

    尽量避免将选项卡粘贴到代码块中,因为它们可能会弄乱格式。我已经为您解决了这个特殊情况。您至少需要提供
    节点
    类的声明,因为您的代码没有显示任何
    字符串
    用法。这可能有助于查看tem的用法plate类不使用
    T
    值(例如,它是否存储任何as字段或对其调用方法);你展示的部分没有使用
    t
    来做任何特殊的事情。还要注意的是,在析构函数中像
    len=0;
    这样的简单赋值是没有意义的。根据你展示的内容,我猜你的
    String
    可能违反了“三个规则”,但我需要看更多的东西才能确定。“如果你使用int类型”在哪里?请发布一个自包含的示例,说明什么地方出错,什么地方可以避免问题。我想看看你的字符串的构造函数(所有的)还有作业运算符。除了你们班的定义之外,当然我非常感谢你们指出这一点。我直接从教授那里抄了这门课,无意中抄了两行……一行删除了当前的代表,另一行创建了一个新的代表。Big woops!!谢谢。我从中学到了很多。@DeadMG给傅老师的好建议是的,但是现在通过编写和维护这些课程所获得的价值是,我希望我能为期中考试做好充分的准备。即使我没有,stack overflow今天也能教我一些东西。我不是最好的调试器,我还在学习基本的编程概念,我已经学习了将近一周了,现在是关键时刻。。
    //Default Constructor
    String::String(){
    rep = new char[1];
    rep[0] = '\0';
    len = 0;
    }  
    
    //Constructor - Converts char* to String object
    String::String(const char *s){
    len=0;
    const char *temp = s;
    while(*temp){
        ++len;
        ++temp;
    }//Sets len of rep to the length of s
    rep = new char[len + 1];
    for(int i=0; i<=len; ++i)
        rep[i]=s[i];
    }
    
    //Copy Constructor
    String::String(const String &obj){
    len=0;
    char *temp = obj.rep;
    
    while (*temp){
        ++len;
        ++temp;
    }//Sets len of rep to length of obj.rep
    rep = new char[len + 1];
    for (int i = 0; i<=len; ++i)
        rep[i] = obj.rep[i];
    }
    
    //Assignment operator
    const String& String::operator=(const String &rhs){
    if (this != &rhs){
        len=0;
        char *temp = rhs.rep;
        while(*temp){
            ++len;
            ++temp;
        }//Sets len of this to length of rhs.rep
        for(int i = 0; i<=len;++i)
            rep[i]=rhs.rep[i];
    }
    return *this;
    }