-2

I'm a student and I'm doing a static library for arrays in C++, so I don't have to rewrite code every time during lessons.

I'm at second year in a secondary school so I'm not an expert. I want my code to be compatible with all type (int, float, ecc.) but I'm having some trouble.

Can you give a look at my code?

I have looked at this page: Template (C++) - not sure if correct

My_vec.h

/*template My_vec.h
Ben Burk
Header file for a vector data structure.
*/

#include <ostream>

using namespace std;

template <typename T> class My_vec
{
    //member variables
    int size, capacity;
    T *ptr;

public:
    //member functions
    template <typename T> My_vec<T>::My_vec()
    {
        size = 0; capacity = 10;
        ptr = new T[capacity];
    }
    template <typename T> My_vec<T>::My_vec(const My_vec<T>& vec)
    {
        if (!vec.is_empty())
        {
            size = vec.size;
            capacity = vec.capacity;
            ptr = new T[capacity];

            for (int i = 0; i < capacity; i++)
                ptr[i] = vec.ptr[i];
        }
    }
    template <typename T> My_vec<T>::~My_vec()
    {
        delete[] ptr;
    }
    template <typename T> My_vec<T>& My_vec<T>::operator=(const My_vec<T>& vec)
    {
        if (this == &vec)
            return *this;   

        this->size = vec.size; this->capacity = vec.capacity;
        this->ptr = new T[vec.capacity];

        for (int i = 0; i < this->capacity; i++)
            this->ptr[i] = vec.ptr[i];
    }
    template <typename T> int My_vec<T>::get_size() const
    {
        return size;
    }
    template <typename T> int My_vec<T>::get_capacity() const
    {
        return capacity;
    }
    template <typename T> T& My_vec<T>::operator[](int i) const
    {
        if (i < 0 || i > capacity)
        {
            try { throw i; }
            catch (int e)
            {
                cerr << "An exception occurred at index: " << e << '\n';
                cerr << "Index out of bounds\n";

            }  
        }
        else
        {
            return ptr[i];
        }
    }
    template <typename T> T& My_vec<T>::operator[](int i)
    {
        if (i < 0 || i > capacity)
        {
            try { throw i; }
            catch (int e)
            {
                cerr << "An exception occurred at index: " << e << '\n';
                cerr << "Index out of bounds!\n";
            }
        }
        else
        {
            return ptr[i];
        }
    }
    template <typename T> bool My_vec<T>::is_empty() const
    {
        return (size == 0) ? 1 : 0;
    }
    template <typename T> T& My_vec<T>::elem_at_rank(int r) const
    {
        if (r <= 0 || r > capacity)
        {
            try { throw r; }
            catch (int e)
            {
                cerr << "An exception occurred at rank: " << e << '\n';
                cerr << "Element could not be found!\n";
            }
        }
        else
            return ptr[r-1];
    }
    template <typename T> void My_vec<T>::insert_at_rank(int r, const T& elem)
    {
        if (r <= 0 || r > capacity)
        {
            try { throw r; }
            catch (int e)
            {
                cerr << "An exception occurred at rank: " << e << '\n';
                cerr << "Element could not be inserted!\n";
            }
        }
        else
        {
            if (size + 1 > capacity)
            {
                capacity *= 2;
                ptr = new T[capacity];
            }
            size++;

            for (int i = size - 1; i > r - 1; i--)
            {
                ptr[i] = ptr[i-1];
            }
            ptr[r-1] = elem;
        }
    }
    template <typename T> void My_vec<T>::replace_at_rank(int r, const T& elem)
    {
        if (r <= 0 || r > capacity)
        {
            try { throw r; }
            catch (int e)
            {
                cerr << "An exception occurred at rank: " << e << '\n';
                cerr << "Element could not be replaced!\n";
            }
        }
        else
        {
            if (ptr[r-1] == NULL)
                size++;

            ptr[r-1] = elem;
        }
    }
    template <typename T> void My_vec<T>::remove_at_rank(int r)
    {
        if (r <= 0 || r > capacity)
        {
            try { throw r; }
            catch (int e)
            {
                cerr << "An exception occurred at rank: " << e << '\n';
                cerr << "Element could not be removed!\n";
            }
        }
        else
        {
            for (int i = r-1; i < size; i++)
            {
                ptr[i] = ptr[i+1];
            }
            size--;
        }
    }
};

ostream& operator<<(ostream& out, const My_vec<T>& vec);
int find_max_index(const My_vec<T>& v, int size);
void sort_max(My_vec<T>& vec);

My_vec.cpp

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

using namespace std;

ostream& operator<<(ostream& out, const My_vec<T>& vec)
{   
    out << "<";
    for (int x = 0; x < vec.get_capacity()-1; x++)
        out << vec[x] << ",";
    out << vec[vec.get_capacity()-1];
    out << ">\n";
}
int find_max_index(const My_vec<T>& v, int size)
{
    int i = -1;
    for (int x = 0; x < v.get_size(); x++)
    {
        if (v[x+1] > v[x])
            i = x+1;
    }
    return i;
}
void sort_max(My_vec<T>& vec)
{
    T c = NULL;
    for (int a = 0; a < vec.get_capacity(); a++)
    {
        for (int x = 0; x < vec.get_capacity()-1; x++)
        {
            if (vec[x+1] < vec[x])
            {
                c = vec[x]; vec[x] = vec[x+1]; vec[x+1] = c;
            }   
        }
    }
}

EDIT: I am having trouble compiling the following files. My assignment is to create a class that can take multiple variable types char, int, double. My question is why can't I compile this program?

Community
  • 1
  • 1
user3799906
  • 51
  • 1
  • 1
  • 6

1 Answers1

1
  1. Remove the template My_vect:: from inside the class.
  2. Make the global functions template functions, otherwise they can't use the template class as parameter. Put their implementation into the header file.
  3. Delete My_vec.cpp, as it's no longer contains any code.

Your header file should look like this:

/*template My_vec.h
Ben Burk
Header file for a vector data structure.
*/

#include <iostream>

using namespace std;

template <typename T> class My_vec
{
    //member variables
    int size, capacity;
    T *ptr;

public:
    //member functions
    My_vec()
    {
        size = 0; capacity = 10;
        ptr = new T[capacity];
    }

    My_vec(const My_vec<T>& vec)
    {
        if (!vec.is_empty())
        {
            size = vec.size;
            capacity = vec.capacity;
            ptr = new T[capacity];

            for (int i = 0; i < capacity; i++)
                ptr[i] = vec.ptr[i];
        }
    }

    ~My_vec()
    {
        delete[] ptr;
    }

    My_vec& operator=(const My_vec<T>& vec)
    {
        if (this == &vec)
            return *this;

        this->size = vec.size; this->capacity = vec.capacity;
        this->ptr = new T[vec.capacity];

        for (int i = 0; i < this->capacity; i++)
            this->ptr[i] = vec.ptr[i];
    }

    int get_size() const
    {
        return size;
    }

    int get_capacity() const
    {
        return capacity;
    }

    T &operator[](int i) const
    {
        if (i < 0 || i > capacity)
        {
            try { throw i; }
            catch (int e)
            {
                cerr << "An exception occurred at index: " << e << '\n';
                cerr << "Index out of bounds\n";

            }
        }
        else
        {
            return ptr[i];
        }
    }

    T &operator[](int i)
    {
        if (i < 0 || i > capacity)
        {
            try { throw i; }
            catch (int e)
            {
                cerr << "An exception occurred at index: " << e << '\n';
                cerr << "Index out of bounds!\n";
            }
        }
        else
        {
            return ptr[i];
        }
    }

    bool is_empty() const
    {
        return (size == 0) ? 1 : 0;
    }

    T& elem_at_rank(int r) const
    {
        if (r <= 0 || r > capacity)
        {
            try { throw r; }
            catch (int e)
            {
                cerr << "An exception occurred at rank: " << e << '\n';
                cerr << "Element could not be found!\n";
            }
        }
        else
            return ptr[r - 1];
    }

    void insert_at_rank(int r, const T& elem)
    {
        if (r <= 0 || r > capacity)
        {
            try { throw r; }
            catch (int e)
            {
                cerr << "An exception occurred at rank: " << e << '\n';
                cerr << "Element could not be inserted!\n";
            }
        }
        else
        {
            if (size + 1 > capacity)
            {
                capacity *= 2;
                ptr = new T[capacity];
            }
            size++;

            for (int i = size - 1; i > r - 1; i--)
            {
                ptr[i] = ptr[i - 1];
            }
            ptr[r - 1] = elem;
        }
    }

    void replace_at_rank(int r, const T& elem)
    {
        if (r <= 0 || r > capacity)
        {
            try { throw r; }
            catch (int e)
            {
                cerr << "An exception occurred at rank: " << e << '\n';
                cerr << "Element could not be replaced!\n";
            }
        }
        else
        {
            if (ptr[r - 1] == NULL)
                size++;

            ptr[r - 1] = elem;
        }
    }

    void remove_at_rank(int r)
    {
        if (r <= 0 || r > capacity)
        {
            try { throw r; }
            catch (int e)
            {
                cerr << "An exception occurred at rank: " << e << '\n';
                cerr << "Element could not be removed!\n";
            }
        }
        else
        {
            for (int i = r - 1; i < size; i++)
            {
                ptr[i] = ptr[i + 1];
            }
            size--;
        }
    }
};

template <typename T>
ostream& operator<<(ostream& out, const My_vec<T>& vec)
{
    out << "<";
    for (int x = 0; x < vec.get_capacity() - 1; x++)
        out << vec[x] << ",";
    out << vec[vec.get_capacity() - 1];
    out << ">\n";
}

template <typename T>
int find_max_index(const My_vec<T>& v, int size)
{
    int i = -1;
    for (int x = 0; x < v.get_size(); x++)
    {
        if (v[x + 1] > v[x])
            i = x + 1;
    }
    return i;
}

template <typename T>
void sort_max(My_vec<T>& vec)
{
    T c = NULL;
    for (int a = 0; a < vec.get_capacity(); a++)
    {
        for (int x = 0; x < vec.get_capacity() - 1; x++)
        {
            if (vec[x + 1] < vec[x])
            {
                c = vec[x]; vec[x] = vec[x + 1]; vec[x + 1] = c;
            }
        }
    }
}

I guess it's a start, but there's still a lot of cleaning up to do, e.g.:

  1. You should not use using namespace in a header file.
  2. What's this?

        try { throw r; }
        catch (int e)
        {
            cerr << "An exception occurred at rank: " << e << '\n';
            cerr << "Element could not be found!\n";
        }
    

    Using exceptions like this doesn't make any sense.

  3. T& operator[](int i) const function. This should return const T&, instead of T&
Bedford
  • 1,136
  • 2
  • 12
  • 36