-1

I'm working on creating an "Array_Base" interface that will allow a user to create either a fixed array or an expandable array of any type. Right now I can't even get the regular Array to work.

I've broken the problem down to a few of its simplest components to try and isolate the issue. I believe it has something to do with my instantiation. I'm using Visual Studio to run the code.

Array_Base.h

#ifndef _ARRAY_BASE_H_
#define _ARRAY_BASE_H_
#include <cstring>          // for size_t definition

template <typename T>
class Array_Base 
{
public:
        typedef T type;
    //Default Constructor
    virtual void Array_Base(void) = 0;

    // Destructor.
    virtual ~Array_Base(void) = 0;

protected:
    /// Pointer to the actual data.
    T* data_;

    /// Current size of the array.
    size_t cur_size_;

    /// Maximum size of the array.
    size_t max_size_;
};

#endif   // !defined _ARRAY_H_

Array.h

#ifndef _ARRAY_H_
#define _ARRAY_H_
#include <cstring>          // for size_t definition
#include "Array_Base.h"

template <typename T>
class Array : public Array_Base
{
    public:
    /// Type definition of the element type.
    typedef T type;

    /// Default constructor.
    Array (void);

        ///Destructor
    ~Array (void);
};

#include "Array.cpp"
#include "Array.inl"
#endif   // !defined _ARRAY_H_

Array.cpp

#include <stdexcept>         // for std::out_of_bounds exception
#include <iostream>
#define MAX_SIZE_ 20

template <typename T>
Array <T>::Array (void)
        :data_(new T[MAX_SIZE_]),
    cur_size_(0),
    max_size_(MAX_SIZE_)
{       }

template <typename T>
Array <T>::~Array (void)
{
    delete[] this->data_;
    this->data_ = nullptr;
}

Main.cpp:

#include "Array.h"

int main(void)
{   
    Array_Base<int>* arr = new Array<int>();
        delete arr;
}

I keep getting an error that says: "a value type of "Array" cannot be used to initialize an entity of type "Array_Base" from a red line that appears under the "new" operator in main.

Any help would be greatly appreciated. Thank you!

bonzi
  • 1
  • 1
  • Not the only problem with your code, but have a look at [why can templates only be implemented in the header](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – super Oct 06 '19 at 06:33

1 Answers1

0
template <typename T>
class Array : public Array_Base

Array_Base does not name a class. You need to provide it with a template argument.

Do you mean this?

template <typename T>
class Array : public Array_Base<T>
ph3rin
  • 4,426
  • 1
  • 18
  • 42