首页 > 解决方案 > 无法从 main 调用函数模板,因为没有匹配的函数调用

问题描述

由于某种原因,我的代码无法调用我的模板... XCODE 一直告诉我没有匹配的函数可以调用“extra”

template<class T>
T extra(T prompt, T low, T high) {.....}

int main()
{
string prompt = "hi"
int low = 5;
int high = 50;

int value = extra(prompt, low, high);

标签: c++

解决方案


template<class T>
T extra(T prompt, T low, T high) { ... }

定义了一个函数,它接受 3 个类型T的参数,所以所有的参数必须是相同的类型。当你打电话时情况并非如此

//                string  int  int
int value = extra(prompt, low, high);

可以这样想:您可以将所有T替换为std::string或将它们全部替换为int。在这两种情况下,参数都不匹配。

但是我无法提出解决方案,因为我不确定您打算做什么。


推荐阅读