简体   繁体   中英

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:

#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...

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. 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.

It also explains why the error goes away when you change main to use the copy constructor instead of the assignment operator.

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. 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:

#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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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