Passing pointer to template class object to a function correctly

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

Passing pointer to template class object to a function correctly



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 ptr 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:



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



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



My questions are:



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.


someFunction



What is the correct synthax to call someFunction in main?



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




1 Answer
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.


someFunction



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


someFunction


template


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.





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
48 mins ago





@nuno95 - Yes, it was a typo. And if you get a linker error it means you forgot templates must be implemented in a header.
– StoryTeller
46 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Mass disable jenkins jobs