Got this开发者_如何学Go in "char.h"
#ifndef _CHAR_H_
#define _CHAR_H_
#include <stdio.h>
template <unsigned int TChar>
class Char
{
public:
Char(){ *m_data=0; m_len=-1; }
private:
char m_data[TChar+1];
int m_len;
};
#endif
Now with this simple test :
#include "char.h"
void test(Char<TChar> &oo)
{
//...
}
int main(int argc, char *argv[])
{
Char<80> gg;
return 0;
}
I get with gcc : TChar was not declared in that scope !? I don't understand, the declaration is in the .h ??
Thanks...
The full implementation of a template class must be in that template class's header (otherwise you are likely to get a linker error). The compiler needs to have access to the entire template definition (not just the signature) in order to generate code for each instantiation of the template, so you need to move the definitions of the functions to your header. (Inclusion Model).
You have placed the definition correctly. :-)
However in void test(Char<TChar> &oo)
the compiler doesn't know what TChar
is. Try adding template <unsigned int TChar>
above the definition
template <unsigned int TChar>
void test(Char<TChar> &oo){
// ... stuff
}
You seem to be confused about what template class is. When you write:
template <unsigned int TChar>
class Char
{
/* ... */
};
You're telling the compiler how to generate classes named as Char<somenumber>
. Every time you use Char<1>
, Char<2>
, ... with different parameters somewhere in your code, the compiler will create a new class for you.
The name TChar
only represents the value that is to be given as the part of the type name and is valid only inside your class template.
This means that when you write Char<80>
the compiler looks if he already has that type - which he doesn't, so he goes and creates a new class named Char<80>
based on your template. That's why you can't write a function that takes any Char because Char<80>
and Char<81>
are different types.
What others are suggesting is that you can turn your function into a function template.
// compiler can't know what value should be used here
// it expects an unsigned int inside <>
void test(Char<TChar> &oo)
{
//...
}
With function templates it works the same as with classes.
template<unsigned int TChar>
void test(Char<TChar> &oo)
{
//...
}
Now when you write
Char<80> gg;
test(gg);
The compiler looks at the type of gg, sees that it can match it with your function template, creates a function with TChar
evaluated as 80 and all works out nicely :)
To be precise, the call to your function should look like this:
test<80>(gg);
but you don't have to say this explicitly, because the compiler has enough information from the type of gg
to work that all out for you.
If you want to pass Char
instantiations to non-templated functions, you can achieve this with polymorphism like this:
class CharBase {
public:
virtual char get(int index) = 0;
virtual ~CharBase() {} // virtual destructor is strongly recommended
};
template<unsigned int CharT>
class Char : public CharBase {
public:
char get(int index) { return m_data[index]; }
/* ... */
};
void test(CharBase &myChar) { /* ... */ }
void test(Char<TChar> &oo)
{
//...
}
TChar
is undefined for this function to be compiled. The file char.h doesn't define TChar
.
精彩评论