Passing pointer to template class object to a function correctly
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKPz5qhCkKGdpPJ0wkSm5JqlliSJoZg92-H4DuCruovhkVFuXerCphZt9wys_Gr3uwQyXObMHDwattjG5BSeCEl_LdDq1GPLct8qYyRyX4fZ9C93oW-TX7IGZLYDwqRgfFCXA4aUxkBAcS/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6VebLk98DYwS3K2nWZSCatab-q4HrSuRk91M4fQA8SHrKU_-x-lq1n780pSU_bIVLqMRv6pc3dhhh-Y2PUwBS9mM0BxvY1eM6XsM-e7Diu4z-wAoeDG0vB3-2_1U9RE8sO0lg4PHiIXbI/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
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.
@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.
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