-1

The class defines objects that are DIMXDIM dynamic two-dimensional arrays that contain objects of any type. Ive added c'tor, d'tor, operator =, operator () that allows me to get the value of (2,5) column/row. and a main that demonstrates the action of T from type float and complex.

I have tried to change the declaration of the c'tor but i think it is not the issue. im really lost

#include<iostream>
#include<cassert>
using namespace std;
//#define DIM 3


template<class T, int DIM>
class Array {
    T **p;
public:
    template<class T, int DIM>
    class Array(T** p) {
        p = new T*[DIM];
        assert(p != NULL);
        for (int i = 0; i < DIM; i++)
        {
            p[i] = new T[DIM];
            assert(p[i] != NULL);
        }
        for (int i = 0; i < DIM; i++) {
            for (int j = 0; j < DIM; j++) {
                p[i][j] = 3;
            }
        }

    }
    template<class T, int DIM>
    class ~Array() {
        for (int i = 0; i < DIM; i++) {
            delete[]p[i];
        }
        delete[]p;
    }
    template<class T, int DIM>
    Array& operator=(const Array& other) {

        if (this != other) {
            for (int i = 0; i < DIM; i++) {
                delete[]p[i];
            }
            delete[]p;

            this.p = new T*[DIM];
            assert(p != NULL);
            for (int i = 0; i < DIM; i++)
            {
                p[i] = new T[DIM];
                assert(p[i] != NULL);
            }


            for (int i = 0; i < DIM; i++) {
                for (int j = 0; j < DIM; j++) {
                    p[i][j] = other.p[i][j];
                }
            }
        }
    }
};

int main() {
    Array<int, 3> ob[][];
    return 0;
}

E0065 expected a ';' Project C:\Users\Lorine E0070 incomplete type is not allowed Project E0098 an array may not have elements of this type C2988 unrecognizable template declaration/definition
C2059 syntax error: 'type'
C2334 unexpected token(s) preceding '{'; skipping apparent function body C2332 'class': missing tag name
C3306 '': unnamed class template is not allowed
C2988 unrecognizable template declaration/definition C2143 syntax error: missing ';' before '~'

  • `template class Array(T** p) {` - this really should be a constructor. I think, you need to read a good C++ book. Choose from those: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – SergeyA Jul 12 '19 at 17:55

1 Answers1

1

There are a few separate errors, but we'll go through them one by one.

Problem. This is the wrong syntax to define a constructor. It looks like you're defining both a class and a function at the same time, which the compiler finds very confusing.

    template<class T, int DIM>
    class Array(T** p) {
        // stuff...
    }

Correction. We already have T defined as a template parameter, so we don't need to re-define it. It should be:

    Array(T** p) {
        // stuff...
    }

Problem. A destructor should never be templated, and you shouldn't put 'class' before it:

    template<class T, int DIM>
    class ~Array() {
        for (int i = 0; i < DIM; i++) {
            delete[]p[i];
        }
        delete[]p;
    }

Correction. Just remove template<class T, int DIM> and class:

    ~Array() {
        for (int i = 0; i < DIM; i++) {
            delete[]p[i];
        }
        delete[]p;
    }

Problem. Unnecessary template before = operator. You only need to template an = operator if the input type depends on the template.

    template<class T, int DIM>
    Array& operator=(const Array& other) {
        // stuff...
    }

Correction. We can just remove the template<class T, int DIM>, since it's unnecessary.

    Array& operator=(const Array& other) {
        // stuff...
    }

Problem. You're declaring a multidimensional c-style array, without specifying the dimensions (c-style arrays must have all (or all but the first) of their dimensions specified at the point they're declared, when used as a local variable).

int main() {
    Array<int, 3> ob[][];
    return 0; 
}

Solution. Either specify the dimensions of the array, or don't use a c-array. (I don't know which one fits your needs. If you post more information I could update the answer to clarify)

Problem. There's no default-constructor

Solution. Add a default constructor after public:

    // stuff...
   public:
    Array() {
        p = new T*[DIM]; 
        // More stuff...
    }

Problem. You immediately discard your parameter in the Array(T** p) constructor:

    Array(T** p) {
        p = new T*[DIM]; // WTF?? Whatever parameter was passed gets thrown out the window
        assert(p != NULL);
        // stuff... 
    }

Solution: Do something with the parameter, instead of just discarding it

Alecto Irene Perez
  • 10,321
  • 23
  • 46