mardi 19 avril 2022

How to initialize a pointer to an array in a class constructor?

I have this code

template <class ItemType>
class ArrTest {
public:
    ArrTest();
private:
    ItemType* info;
};

template <class ItemType>
ArrTest<ItemType>::ArrTest()
{
    info = ItemType[50];
}

int main() {
    ArrTest<int> ar();
    return 0;
}

Right now when I try and build this I get

../src/test1.cpp:23:9: error: 'ItemType' does not refer to a value
        info = ItemType[50];

I don't understand how to initialize this into pointer. I believe its a pointer to the first item in the array. but then I should also be able to do info[3] for the 4th member of the array for instance.

Aucun commentaire:

Enregistrer un commentaire