Question

So what I'm trying to do is write a class with a template class as its data member. This is part of my homework.

So far this is what I have:

letter_frequencies.h

#include "ltr_freq_pair.h"
#include "darray.h"

class letter_frequencies
{
    private:
        darray<letterFrequencyStruct> array;
    public:
        letter_frequencies();
        void outputFrequencies();
};

letter_frequencies.cpp

#include <iostream>
#include "letter_frequencies.h"

letter_frequencies::letter_frequencies()
{}

void letter_frequencies::outputFrequencies()
{
    for(int index = 0; index < 26; index ++)
    {
        std::cout << "\n" << array[index].letter;
        std::cout << " " << array[index].frequency;
    }
}

Everything compiles, but once I run the program it has this error:

a.out: darray.cpp:44: T& darray<T>::operator[](unsigned int) [with T = letterFrequencyStruct]: Assertion 'index < used' failed. Abort (core dumped)

The code snippet I'm trying to use for this class is:

    //Check if Class Works
    letter_frequencies array;

    array.outputFrequencies();

Right now, the darray works just fine and the program does exactly what I need it to do, but one of the requirements is to have a class named letter_frequency with a data member darray<structTypename> name, which is why I'm fussing with this in the first place.

Also, how would I make the class call the explicit constructor for the darray? I want to set the size to 26.

darray.h

#ifndef DARRAY_H
#define DARRAY_H

#include <iostream>

template <typename T>
class darray
{

    public:
        //Constructors
        darray();
        darray(const darray&);
        explicit darray(unsigned);

        //Destructor
        ~darray();

        //Constant Members
        unsigned size() const {return used;}
        unsigned capacity() const {return cap;}

        //Modification Members
        T& operator [] (unsigned);
        void pop_back();
        void push_back(const T&);
        const T* find(const T&) const;
        void resize(unsigned);

        //Operators
        T& operator = (const darray&);
        bool operator == (const darray&) const;

        //Friends
        template <typename X>
        friend std::ostream& operator << (std::ostream& output, const darray<X>& array);

    private:
        T* arr;
        unsigned used, cap;

};

template <typename X>
std::ostream& operator << (std::ostream& output, const darray<X>& array)
{
    for(int index = 0; index < array.size; index++)
    {
        output << index << array[index];
    }

    return output;
}

#include "darray.cpp"

#endif
Was it helpful?

Solution

As I mentioned before, you'd have to make letter_frequencies constructor like this:

letter_frequencies::letter_frequencies() : array(26) {
}

(so you initialize the array with the necessary number of elements).

BUT: I don't think you should make your own home-grown array when std::vector would be better...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top