[Bf-committers] problems with C++
Jonathan Merritt
j.merritt at pgrad.unimelb.edu.au
Fri Sep 30 15:57:19 CEST 2005
Nathan Letwory wrote:
>>I'm not sure if this matters much, but all of the functions are defined
>>in lbmfsgrsolver.h. This isn't the place to define the functions, right?
>>Maybe that could be a problem.
>>
>>
>
>I learned just today that it is in this case the correct way. See [1] for
>info. I suffered from undefined references in my own C++ code, and
>browsing through Google yielded [1] as a reason to why.
>
You can (with GCC at least) also explicitly instantiate your template
classes if you know in advance what types you'll be using. Then you can
define components of templates outside of header files. I don't know if
this is "good" or even standards compliant, but it works! :-)
For example:
//=== In a header file ===
template <typename T> class MyClass {
public:
void myfun();
};
//=== In a CPP file ===
template <typename T>
void MyClass::myfun() {
// do stuff
}
// Some explicit instantiations
template class MyClass<int>;
template class MyClass<float>;
You can then use MyClass<int> and MyClass<float>, but not (for example)
MyClass<double> anywhere else in the program.
Jonathan Merritt.
More information about the Bf-committers
mailing list