C++ 编译模板函数时形式参数列表不匹配
编译模板函数时出现编译时错误,错误为: 错误C2563:形式参数列表中不匹配 不知道问题出在哪里,编译器没有告诉你多少,你看到问题出在哪里了吗C++ 编译模板函数时形式参数列表不匹配,c++,template-specialization,function-templates,C++,Template Specialization,Function Templates,编译模板函数时出现编译时错误,错误为: 错误C2563:形式参数列表中不匹配 不知道问题出在哪里,编译器没有告诉你多少,你看到问题出在哪里了吗 #include <cmath> // temporary #include <iostream> #include <cassert> namespace math { // // Power function // template<typename Exp>
#include <cmath> // temporary
#include <iostream>
#include <cassert>
namespace math
{
//
// Power function
//
template<typename Exp>
double pow(double base, Exp exponent)
{
assert(!(base == 0 && exp <= 0));
if (base == 0)
return 0;
if (exponent == 0 || base == 1)
return 1;
if (exponent == 1)
return base;
if (exponent < 0)
return 1 / pow(base, -exponent);
return base * pow(base, exponent - 1);
}
//
// Power specialization for real exponents
//
template<>
double pow(double base, double exponent)
{
// TODO: handle real negative exponents
return exp(exponent * log(base));
}
}
int main()
{
// error C2563: mismatch in formal parameter list
std::cout << "pow" << std::endl;
std::cout << math::pow(1, 2) << std::endl;
std::cout << math::pow(0, 2) << std::endl;
std::cout << math::pow(2, 0) << std::endl;
std::cout << math::pow(3, -4) << std::endl;
return 0;
}
#包括//临时
#包括
#包括
名称空间数学
{
//
//幂函数
//
模板
双功率(双基,经验指数)
{
assert(!(base==0&&exp我想我应该写一个答案给别人看
在Martin Morterol写的评论中,他解释了为什么会出现错误
您正在将exp函数与0进行比较。
assert(!(base==0&&exp我想我应该写一个答案给别人看
在Martin Morterol写的评论中,他解释了为什么会出现错误
您正在将exp函数与0进行比较。
assert(!(base==0&&exp断言中的exp
是什么?将0与function@lubgr我看到应该是exponent
我重命名了参数,但是忘记了,编译器不是应该报告未声明的标识符吗?嗯,除了s中声明的之外,还有一个exp
函数,一些编译器提供了这个函数td
.Error from clang:“main.cpp:13:35:错误:指针和零之间的有序比较('double()(double)throw()'和'int')”*断言中的exp
是什么?将0与function@lubgr我看到应该是exponent
我重命名了参数,但忘记了,编译器不应该报告未声明的标识符吗?嗯,除了在std
中声明的之外,还有一个exp
函数,一些编译器还提供了rom叮当声:“main.cpp:13:35:错误:指针和零之间的有序比较('double()(double)throw()'和'int')”*