我已经在网上搜索过,但我找不到我的问题的答案> 为什么C++模板代码无法编译?一旦我删除了 return 语句之前的最后一行,它就会按预期编译和运行。
我使用 g++ 版本 4.3.4。我将非常感谢专家的帮助。
问候, 迈卡
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <stdint.h>
#include <boost/lexical_cast.hpp>
void mult(int* ptr)
{
std::cout << "void mult(" << *ptr << "): line: " << __LINE__ << ", function: "<< __FUNCTION__ << std::endl;
}
template <typename T>
void multIndirect(T* x)
{
std::cout << "void mult(" << *x << "): line: " << __LINE__ << ", function: "<< __FUNCTION__ << std::endl;
}
void apply(void (*funcPtr)(int*), int* pVal)
{
std::cout << "apply(" << boost::lexical_cast<std::string>((uintptr_t) funcPtr) << ", " << *pVal << "): line:" << __LINE__ << ", function: " << __FUNCTION__ << std::endl;
funcPtr(pVal);
}
template <typename Func, typename T>
void applyIndirect(Func funcPtr, T* x)
{
std::cout << "apply(" << boost::lexical_cast<std::string>((uintptr_t) funcPtr) << ", " << *x << "): line:" << __LINE__ << ", function: " << __FUNCTION__ << std::endl;
funcPtr(x);
}
int main(void) {
int y = 300;
mult(&y);
apply(mult, &y);
apply(multIndirect, &y);
applyIndirect(multIndirect, &y);
return EXIT_SUCCESS;
}
我得到编译器错误:
CPPTemplate.cpp: In function int main():
CPPTemplate.cpp:47: error: no matching function for call to applyIndirect(<unresolved overloaded function type>, int*)
make: *** [CPPTemplate.o] Error 1