没有匹配的调用函数

No matching function for call?

本文关键字:调用 函数      更新时间:2023-10-16

我不知道为什么我得到这个错误:

error: no matching function for call to ‘write_vector(int&)’
    template <typename T>
    void write_vector(const vector<T>& V)
    {
       cout << "The numbers in the vector are: " << endl;
      for(int i=0; i < V.size(); i++)
        cout << V[i] << " ";
    }
    int main()
    {
      int input;
      vector<int> V;
      cout << "Enter your numbers to be evaluated: " << endl;
      cin >> input;
      V.push_back(input);
      write_vector(input);
      return 0;
    }

Write

write_vector(V); //V is std::vector<int>

代替

write_vector(input); //input is int

请尝试理解错误信息。它提供了很多提示,告诉你代码中哪里出了问题。

而且,你似乎在代码的某个地方写了using namespace std。不要那样做。把那条线擦掉。然后用std::vector代替vector。这是一种较好的编码方式,可以避免using namespace std带来的许多问题,特别是在大型项目中。

将输入变量传递给函数,模板函数需要一个向量。试着用V代替

使用错误的形参调用write_vector函数。在您刚刚发布的代码中,您正在使用'input'作为参数调用过程,但'input'是'int'类型而不是'vector'。