-2

arrayADT.h

 #include <iostream>
    using namespace std;
    
    
    template <class T>
    class arrayADT
    {
    private:
        T *A;
        static int size;
        static int length;
    public:
        arrayADT(){
            size=10;
            A= new T[size];      
            length=0;
        }
    
        void increaseSize(){
            T *p;
            size=size*2;
            p= new T[size];
            delete[] A;
            A=p;
            p=NULL;
        }
    
        int getSize(){
            return size;
        }
    
        ~arrayADT();};

example.cpp

#include<iostream>
#include<stdio.h>
#include"arrayADT.h"
using namespace std;

int main(int argc, char const *argv[])
{   
    arrayADT<int> s;
    s.increaseSize();
    s.getSize();
    return 0;
}

Get error:

undefined reference to `arrayADT::~arrayADT()'

undefined reference to `arrayADT::~arrayADT()'

undefined reference to `arrayADT::length'

undefined reference to `arrayADT::size'

Can anyone help me? Thanks very much!

1 Answers1

0

I found two different problems:

  1. The destructor method must have a scope with {} even if it is empty.
  2. I removed the static keyword as each object will have size and length property.
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;
    
template<class T>
class arrayADT
{
    private:
        T *A;
        int size;    /* Removed the static keyword used in data members. */
        int length;  /* Removed the static keyword used in data members. */
        
    public:
        arrayADT()
        {
            size = 10;
            A = new T[size];      
            length = 0;
        }
        
        ~arrayADT(){} /* The destruction method has been edited. */
        
        void increaseSize()
        {
            T *p;
            size = size * 2;
            p = new T[size];
            delete[] A;
            A = p;
            p = NULL;
        }
    
        int getSize()
        {
            return size;
        } 
};

int main(int argc, char const *argv[])
{   
    arrayADT<int> s;
    s.increaseSize();
    cout << s.getSize() << endl;
    return 0;
}
Sercan
  • 4,739
  • 3
  • 17
  • 36