Suri

Suri

  • NA
  • 113
  • 11.2k

Hey my code is giving segmentation error. Can someone help me out with

Jul 14 2021 3:33 PM

Hey my code is giving segmentation error. Can someone help me out with it

using namespace std;

template<typename t>
class hello
{
private:
    t *a;
    t *b;
    t *c;
    int size;
public:

    hello()
    {
        size = 0;
        a = new t[100];
        b = new t[100];
        c = new t[100];
    }
    hello(int s)
    {
        size = s;
        a = new t[size];
        b = new t[size];
        c = new t[2 * size];
        cout << "Enter the contents of the array" << endl;
        for (int i = 0; i < size; i++)
        {
            cin >> a[i];
        }
        cout << "Enter the contents of the array" << endl;
        for (int j = 0; j < size; j++)
        {
            cin >> b[j];
        }
    }
    void unionn()
    {
        for (int i = 0; i < size; i++)
        {
            c[i] = a[i];
        }
        for (int i = 0; i < size; i++)
        {
            c[size] = b[i];
            size++;
        }
        for (int i = 0; i < (2*size); i++)
        {
            cout << c[i];
        }
    }
};

int main()
{   
    int length;
    cout<<"Enter the length of the array"<<endl;
    cin>>length;
    cout<<"Enter the integer contents of the array"<<endl;
    hello <int>c1(length);
    c1.display();
    c1.unionn();
    cout<<"Enter the string contents of the array"<<endl;
    hello <string>c2(length);
    c2.display();
    c2.unionn();
    return 0;
}

 


Answers (3)