Reputation: 190659
I have a template class to print out the elements in vector. I have both for pointer and reference version.
// HEADER
class Util {
...
template <class T>
static void print(const std::vector<T>* vectorArray);
template <class T>
static void print(const std::vector<T>& vectorArray);
...
static void printByteStream(const std::vector<unsigned char>& input);
...
};
// BODY
template <class T>
void Util::print(const std::vector<T>* vectorArray)
{
for (auto i = vectorArray->begin(); i != vectorArray->end(); ++i)
{
std::cout << *i << ":";
}
std::cout << std::endl;
}
template <class T>
void Util::print(const std::vector<T>& vectorArray)
{
return Util::print(&vectorArray);
}
template void Util::print(const std::vector<int>* vectorArray);
template void Util::print(const std::vector<std::string>* vectorArray);
template void Util::print(const std::vector<int>& vectorArray);
template void Util::print(const std::vector<std::string>& vectorArray);
I also have a print code for byte stream.
void Util::printByteStream(const std::vector<unsigned char>& input)
{
for (auto val : input) printf("\\x%.2x", val);
printf("\n");
}
I want to teach the C++ compiler that when I call print with T == unsigned char, call the printByteStream with partial specialization.
I added this code in the body.
void Util::print(const std::vector<unsigned char>& vectorArray)
{
return Util::printByteStream(vectorArray);
}
When compiled, the C++ compiler complains that it can't find matching code. What might be wrong?
error: prototype for 'void Util::print(const std::vector<unsigned char>&)' does not match any in class 'Util'
void Util::print(const std::vector<unsigned char>& vectorArray)
Upvotes: 0
Views: 173
Reputation: 629
I think you need to add an empty template
template <>
void Util::print(const std::vector<unsigned char>& vectorArray)
{
return Util::printByteStream(vectorArray);
}
I have to check. I don't have VS on this computer though.
Upvotes: 2