0

I have some code like this:

template<class T> class classA
{
public:
    classA(uint32_t max) : numElements(max)
    {
        data = new T[numElements];
    }
    ~classA() {delete[] data;}

    uint32_t size()
    {
        return numElements;
    }

    T *data;

private:
    const int numElements;
};

class classB
{
public:
    classB(uint8_t* adress) : ptr(adress){}

    void someFunction(classA<class T>* x)
    {
        uint8_t size = sizeof(T);
        /*
        do some stuff with the type size of T
        */

        T* ptr1 = (T*)ptr; // create new pointer with same adress as ptr but different data type
        /*
        write some data from ptr1[i] to x->A[i]
        */
    }

private:
    uint8_t* ptr;
};


int main()
{
    classB B(/*some memory adress*/);
    classA<uint16_t> A(5)
    B.someFunction(&A);
}

This code (it is very simplified here) should make it possible for an object of classB to write data starting from a specific memory adress to the data field of a classA object no matter which type the data array inside classA has.

As written, the compiler gives some errors:

  1. invalid application of 'sizeof' to incomplete type 'T'

  2. invalid use of incomplete type 'class T' (because of the pointer stuff in someFunction)

  3. invalid arguments ' (because of the call of B.someFunction(&A))
  4. no matching function for call to 'classB::someFunction(classA<>*)'

My questions are:

  1. What do I have to change in order to reach the following functionality? The pointer given to someFunction should be allowed to have an arbitrary data type and the operations shown above should work without incomplete type errors.

  2. What is the correct synthax to call someFunction in main?

I've tried different things already but nothing helped :-(

nuno95
  • 3
  • 2

1 Answers1

0

What do I have to change in order to reach the following functionality? The pointer given to someFunction should be allowed to have an arbitrary data type and the operations shown above should work without incomplete type errors.

So someFunction needs to be a template itself. Therefore it needs the appropriate template header:

template<class T>
void someFunction(classA<T>* x)
{
    uint8_t size = sizeof(T);
    /*
    do some stuff with the type size of T
    */

    T* ptr1 = (T*)ptr; // create new pointer with same adress as ptr but different data type
    /*
    write some data from ptr to x->A[i]
    */
}

Beyond that your syntax looks correct.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • I assume you mean template instead of typename. If I follow your solution, everything works fine but there is an error left for the call of someFunction (undefined reference to ...). – nuno95 Jul 29 '18 at 12:33
  • @nuno95 - Yes, it was a typo. And if you get a linker error it means you forgot [templates must be implemented in a header](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – StoryTeller - Unslander Monica Jul 29 '18 at 12:35