-1

I'm lamenting about a simple function that converts objects from an array-like data structure into a linked list-like one, both being ArchiCAD's own classes.

The question is that is it possible to do it for any kind of objects.

Code looks like this:

GS::Array<class T> *GetItemsFromNeig(API_Neig **p_neigs)
{
    UInt32 nSel =   BMGetHandleSize((GSHandle)p_neigs) / sizeof(API_Neig);
    GS::Array<T>*   resultArray = new GS::Array<T>;

    for (UInt32 ii = 0; ii < nSel; ++ii) {
        resultArray->Push((T) *p_neigs[ii]); //incomplete type is not allowed
    }

    return resultArray;
}

The error is not a surprise, the question is that is it possible to write a function like this.

Gyula Sámuel Karli
  • 3,118
  • 2
  • 15
  • 18
  • Yes, you can lament about any kind of object, if you're so inclined. What exactly is your doubt? What have you tried and found out? You also say you get an error, so please provide a [mcve] and the full error message. – Ulrich Eckhardt Nov 06 '21 at 08:53
  • *The error is not a surprise* Good. Then you should be able to present the reason for why you need the class forward declared at the point of instatiation. We can't guess what your code is if you don't show it. Your code also raises some other questions, like why raw c-arrays? Why do we have a pointer to a `GS::Array`? – super Nov 06 '21 at 09:05
  • 1
    Shouldn't your function be prefixed by `template `? And `GS::Array` should be `GS::Array` instead. – Scheff's Cat Nov 06 '21 at 09:29
  • Scheff's Cat: that was the sollution/that I didn't understand, thanks. – Gyula Sámuel Karli Nov 06 '21 at 12:10

1 Answers1

0

For the log, the answer is:

template <class T>
GS::Array<T> *GetItemsFromNeig(API_Neig **p_neigs)
{
    UInt32 nSel =   BMGetHandleSize((GSHandle)p_neigs) / sizeof(API_Neig);
    GS::Array<T>*   resultArray = new GS::Array<T>;

    for (UInt32 ii = 0; ii < nSel; ++ii) {
        resultArray->Push((T) (*p_neigs)[ii]);
    }

    return resultArray;
}
Gyula Sámuel Karli
  • 3,118
  • 2
  • 15
  • 18