-4

I face this problem:

No source available.
Call stack location: lab5.exe! Counter:: 'scalar deleting destructor'() + 0x2b bytes.

I construct a single linked list.

counter.h

class Counter{
    private:
        char* m_pStr; 
        unsigned int m_nOwners; 
        Counter* pNext;         
        static unsigned int m_curCounters;
        static Counter* Head;   
...

counter.cpp

Counter* Counter:: Head = new Counter();
unsigned int Counter:: m_curCounters = 0;

Counter:: ~Counter(){
    if  (this == Head){
        Head = Head->pNext;
    }
    else{
        Counter* current = Head->pNext;
        for (int i = 0; i < m_curCounters; i++){
            if (current->pNext == this){
                            // Searching for counter, with next one equal this.
                current->pNext = this->pNext;
                break;
            }
            current = current ->pNext;
        }
    }
    m_curCounters--;
    delete[] this->m_pStr;
}

The closing brace leads to the error. In the pictures: one step separates me from the error screen and the error itself.

Screenshot 1 Screenshot 2 Screenshot 3

P.S. Vectors are forbidden.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
Trts
  • 985
  • 1
  • 11
  • 24

1 Answers1

2

Your class needs to follow the Rule of Three.
You need to provide a copy constructor & copy assignment operator which perform deep copies of the dynamically allocated data members.

Alternatively, You can use a smart pointer instead of a raw pointer. This will save you all the explicit manual memory management.

Also, you are much better off replacing char * with std::string.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533