ollo
ollo

Reputation: 25350

Clearing generic array throws logic_error if non-primitive type is used

I have a generic array class which throws a logic_error if it's used with a non-primitive type.

Template Class:

#include <string>
#include <sstream>
#include <iostream>    

using namespace std;

#define NULL_ELEMENT        ((T)NULL)


template<class T> class Array
{    
public:    
    Array(const int size)
    {
        this->elements[size];
        this->size = size;
        ::fill_n(elements, size, NULL_ELEMENT); /* 1 */
    }


    // Output of the array
    string toString()
    {
        int i=0;
        stringstream ss;

        ss << "Array{ ";

        for( ; i<size-1; i++ )
        {
            ss << elements[i] << ", ";
        }

        ss << elements[i] << " }";

        return ss.str();
    }

    // ...

private:
    int size;
    T elements[];
};

Test Code:

Working (primitive type used):

Array<int> arr(5);
cout << arr.toString() << endl;

Array is filled with 0: Array{ 0, 0, 0, 0, 0 }

Fail (non-primitive type used):

Array<string> arr(size); // <-- Exception thrown here
cout << arr.toString() << endl;

Thrown Exception:

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct null not valid

This happens in Array class at when ::fill_() is called (/* 1 */).

I want to fill the whole array with the Null-Element of the type T (like 0 if int or NULL if pointer etc.) - without iterating over each element. memset() is not a good solution here, isnt it?

Upvotes: 2

Views: 330

Answers (1)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361362

Here is what you should be doing. This is minimal code with correct skeleton of your class.

template<class T> 
class Array
{
     T  *m_elements;  //declare a pointer member 
     size_t m_size;   //count of the elements

public:    

    Array(size_t size) : m_size(size), m_element(new T[size]())
    {                // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                     //    use member-initialization-list
    }

    ~Array(); //must define it

    Array(Array const & other); //must define it

    Array& operator=(Array const & other); //must define it

    Array(Array&& temporary); //better define it (in C++11)
    Array& operator=(Array&& temporary); //better define it (in C++11)

    //other
};

To know why you must define the destructor, copy-constructor and copy-assignment, better define move-constructor and move-assignment, see these (in that order):

Upvotes: 4

Related Questions