简体   繁体   English

奇怪的复制构造函数和析构函数错误

[英]Strange copy constructor and destructor error

I have a class and i keep getting some error from the destructor.我有一堂课,我不断从析构函数中得到一些错误。 This is the clas:这是班级:

#pragma once
class Number
{
    int bas;
    char* val;
public:
    Number(const char* value, int base); 
    Number(const Number& x);
    ~Number();
    void SwitchBase(int newBase);
    void Print();
    int  GetDigitsCount();
    int  GetBase(); 
};

This is the cpp file:这是.cpp文件:

#include "Number.h"
#include <iostream>
Number::Number(const char* value, int base)
{
    int a = -1;
    do
    {
        a++;
    } while (value[a] != '\0');
    val = new char[a + 1];
    for (int i = 0; i <= a; i++)
        val[i] = value[i];
    bas = base;
}

Number::Number(const Number& x)
{
    int a = -1;
    bas = x.bas;
    do
    {
        a++;
    } while (x.val[a] != '\0');
    delete[]val;
    val = new char[a + 1];
    int i;
    for (i = 0; i <= a; i++)
        val[i] = x.val[i];
}
Number::~Number()
{
    delete[]val;
}
void Number::Print()
{
    std::cout << "Numarul este: " << val<< std::endl << "Baza este: " << bas<<std::endl;
}
int Number:: GetDigitsCount()
{
    int l = 0;
    do
    {
        l++;
    } while (val[l] != '\0');
    return l;
}

This is the main:这是主要的:

int main()
{
    Number x("123", 10),y("111",10),z("0",10);
    z = y;
    z.Print();
}

I keep getting this error: Invalid address specified to RtlValidateHeap( 010C0000, 010C8DD8 ) If i do this change in main it works properly but it is not really what I want...我不断收到此错误:无效地址指定给 RtlValidateHeap( 010C0000, 010C8DD8 ) 如果我在 main 中进行此更改,它可以正常工作,但它并不是我真正想要的...

int main()
{
    Number x("123", 10),y("111",10);
    Number z = y;
    z.Print();
}

How can I solve this?我该如何解决这个问题? I can't figure it out...我想不通...

Your Number class is missing an assignment operator.您的Number类缺少赋值运算符。 Since you use the assignment operator in main the default assignment operator will cause a double delete when you exit main and this explains the error.由于您在main使用赋值运算符,因此当您退出main时,默认赋值运算符将导致双重删除,这解释了错误。

It also explains why the error goes away when you change main to use the copy constructor instead of the assignment operator.它还解释了当您将 main 更改为使用复制构造函数而不是赋值运算符时错误消失的原因。

You should look at the copy and swap idiom to show how to easily and efficiently implement copy constructors and assignment operators.您应该查看复制和交换习语,以展示如何轻松有效地实现复制构造函数和赋值运算符。

Alternatively you could also use std::string instead of manually allocating memory.或者,您也可以使用std::string而不是手动分配内存。 This would eliminate the need to write a destructor, copy constructor and assignment operator.这将消除编写析构函数、复制构造函数和赋值运算符的需要。 That's the best solution.这是最好的解决办法。

This is an example of how code may look like using std::string:这是一个使用 std::string 的代码示例:

#include <iostream>
#include <string>

class Number
{
    int bas;
    std::string val;
public:
    Number(std::string, int base); 
    Number(const Number& number);
    Number& operator= (const Number& number);
    ~Number()=default;
    void Print();
    int  GetDigitsCount();
};


Number::Number(std::string value, int base)
{
    val=value;
    bas=base;
}

Number::Number(const Number& number)
{
    val=number.val;
    bas=number.bas;
}

Number& Number::operator= (const Number& number)
{
    val=number.val;
    bas=number.bas;
    return *this;
}

void Number::Print()
{
    std::cout << "Numarul este: " << val<< std::endl << "Baza este: " << bas<<std::endl;
}
int Number:: GetDigitsCount()
{
    return val.size();
}

int main()
{
    Number x("123", 10),y("111",10),z("0",10);
    Number k(y);
    k.Print();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM