|
Hi all,
I have this piece of code but I didn't write it, and I'm struggling my butt off to understand it ... can anyone help me...
<br />
typdef struct {<br />
uint8_t status;
uint8_t nrclus;
uint16_t tfmtag;
uint16_t recdid;
uint32_t fladdr;
uint8_t *pdbuff;
} TFM_PARAM3;<br />
<br />
typdef struct {<br />
uint8_t status;
uint8_t nrclus;
uint16_t tfmtag;
uint16_t recdid;
uint32_t fladdr;
uint8_t *pdbuff;
void (*app_cb) (TFM_PARAM3 TfmParameters);
} TFM_PARAM2;<br />
<br />
Many thanx...<br />
<br />
Regards
Programm3r
|
|
|
|
|
Programm3r wrote: void (*app_cb) (TFM_PARAM3 TfmParameters);
this declares a pointer to a function which gets a TFM_PARAM3 as a parameter, and returns nothing. this function pointer is called app_cb in your structure.
to use it, you can do this :
void Foo (TFM_PARAM3);
TFM_PARAM2 myStruct;
myStruct.app_cb = &Foo;
|
|
|
|
|
Thanx alot you guys ....
Regards
Programm3r
|
|
|
|
|
toxcct wrote: void Foo (TFM_PARAM3); TFM_PARAM2 myStruct;myStruct = &Foo;
Shouldn't we write:
myStruct.app_cb = &Foo
----------------------
Mayank Thakore
Learning C++ - since 1998
They didn't print my card right; so I resigned.
|
|
|
|
|
Yes, you're correct.
You can also write:
myStruct.app_cb = Foo;
I think that's also in the standard rather than being a common compiler extension.
Kev
|
|
|
|
|
lemur2 wrote: You can also write:
myStruct.app_cb = Foo;
functions have a unique property that their value is their address
i.e. &Foo == Foo
not sure if this is standard... works in VC++
----------------------
Mayank Thakore
Learning C++ - since 1998
They didn't print my card right; so I resigned.
|
|
|
|
|
MayankT wrote: not sure if this is standard... works in VC++
yes it works under microsoft compilers, but not standard. if i remember correctly, the last standard (or the future one) prevents to use the notation without the address operator...
|
|
|
|
|
toxcct wrote: the last standard (or the future one) prevents to use the notation without the address operator...
this is good to remove the slight confusion...
is there something planned for using function pointers as well?
like:
<br />
voif func () {}<br />
<br />
void (*func_ptr) ();<br />
<br />
func_ptr = &func;<br />
<br />
(*func_ptr)();
func_ptr ();
<br />
----------------------
Mayank Thakore
Learning C++ - since 1998
They didn't print my card right; so I resigned.
|
|
|
|
|
i don't know for this, but i personnaly prefer calling a function (even through a function pointer) with the usual syntax.
if you call a function with func_ptr() (even if the name is not that explicit), you can always browse the sources for the declaration of the func_ptr pointer, and then identify it to being a function pointer...
|
|
|
|
|
yes of course, i typed too fast and forgot to apply the .operator to the field of the structure.
thanks for the notice
|
|
|
|
|
Programm3r wrote: void (*app_cb) (TFM_PARAM3 TfmParameters); // THIS PART I DONT UNDERSTAND
Isn't this pointer to a function returning void and taking parameter to type TFM_PARAM3, which is declared above.
|
|
|
|
|
I have no idea .... what why I posted the question....
Regards
Programm3r
|
|
|
|
|
I have defined a template class in a header file like so:
template <class _T> class TCollection {
public:
TCollection();
~TCollection();
Then try to declare the members in the cpp file like so:
TCollection::TCollection() : pData(0) , pCount(0)
{
}
TCollection::~TCollection()
{
clear();
}
Yet I am getting a compiler error
'TCollection' : use of class template requires template argument list
Obviously it cannot be treated like a normal class, so what is the correct way of declaring the members?
|
|
|
|
|
waldermort wrote: template <class _t=""> class TCollection
BTW, what is pData declared like ?
|
|
|
|
|
TCollection<class _T>::TCollection()
That worked, thankyou.
|
|
|
|
|
I tell a lie, it worked for the c'tor and d'tor but not the other members.
This is the header file:
template <class _T> class TCollection {
public:
TCollection();
~TCollection();
_T at(unsigned int iPos);
void add(_T T);
void clear();
int size();
bool empty();
private:
_T* pData;
int pCount;
};
And the cpp file looks something like this:
TCollection<class _T>::TCollection() : pData(0) , pCount(0)
{
}
TCollection<class _T>::~TCollection()
{
clear();
}
_T TCollection<class _T>::at(unsigned int iPos)
{
if (iPos >= 0 && iPos > pCount)
return pData[iPos];
else
return NULL;
}
void TCollection<class _T>::add(_T T)
{
if (pData) {
_T *tmp = new _T [pCount+1];
for (int i=0;i<pCount;i++)
tmp[i] = pData[i];
tmp[i] = T;
delete [] pData;
pData = tmp;
pCount++;
} else {
pData = new _T [1];
pData[0] = T;
pCount++;
}
}
I'm getting error like
E:\Program\foo\TCollection.cpp(18) : error C2027: use of undefined type '_T'<br />
E:\Program\foo\TCollection.cpp(8) : see declaration of '_T'<br />
E:\Program\foo\TCollection.cpp(20) : error C2036: 'class _T *' : unknown size<br />
E:\Program\foo\TCollection.cpp(28) : error C2512: '_T' : no appropriate default constructor available
This is only after a split it into seperate files, it worked in a single file.
|
|
|
|
|
template<class _T>
_T TCollection::at(unsigned int iPos){...}
template<class _T>
void TCollection::add(_T T){...}
|
|
|
|
|
Thanks for your help with this, but I am still getting errors.
template<class _T> _T TCollection::at(unsigned int iPos)
{
...
}
template<class _T> void TCollection::add(_T T)
Results in the first error:
E:\Program\foo\TCollection.cpp(17) : error C2955: 'TCollection' : use of class template requires template argument list<br />
e:\program\foo\tcollection.h(24) : see declaration of 'TCollection'<br />
E:\Program\foo\TCollection.cpp(25) : error C2955: 'TCollection' : use of class template requires template argument list
|
|
|
|
|
template<class _T>
_T TCollection<_T>::at(unsigned int iPos){...}
template<class _T>
void TCollection<_T>::add(_T T) {...}
|
|
|
|
|
Thankyou that worked, but now having problems with the pData pointer. I thought it would be easy to use seperate files. Rather than messing about I have placed the whole thing in the single header file, it's only a small class.
|
|
|
|
|
template classes inmplementations have to be in the header...
what is the problem with pData exactly ?
|
|
|
|
|
I can't remember eaxactly after putting it back in the header, it was something about an unknown class size when trying to delete it. Basicaly this is just a template similar to the vector class, with the exception it's smaller and calls the destructor.
|
|
|
|
|
toxcct wrote: template classes inmplementations have to be in the header.
This is only because there is no compiler out there that meets this part of the standard (mainly due to the insane complexity it takes to accomplish it). The standard allows for placing template definitions in header files and implementations in cpp files.
To get around this problem, I generally either put the entire class definition and implementation into a single file, or I place a #include "myclass.cpp" at the bottom of my header file.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
yes i know that, but that fact is that actually, so i didn't want to pollute the post with something that wouldn't have helped...
|
|
|
|
|
Probably problem is you have used TCollection class object without using argument list.
You need to use it like this,
TCollection<int> objTCollection;
|
|
|
|