c++ - 没有匹配的函数可供调用

标签 c++ templates function-declaration variable-length-array

我的终端消息

zo@laptop:~/Desktop$ g++ stack.cpp 
stack.cpp: In function ‘int main(int, char**)’:
stack.cpp:50:19: error: no matching function for call to ‘boyInitial(Boy [boyNumber])’
     boyInitial(boy);
                   ^
stack.cpp:17:6: note: candidate: template<class T, int N> void boyInitial(T (&)[N])
 void boyInitial(T (&boy)[N]){
      ^~~~~~~~~~
stack.cpp:17:6: note:   template argument deduction/substitution failed:
stack.cpp:50:19: note:   variable-sized array type ‘long int’ is not a valid template argument
     boyInitial(boy);
                   ^
stack.cpp:51:19: error: no matching function for call to ‘getBoyAges(Boy [boyNumber])’
     getBoyAges(boy);
                   ^
stack.cpp:34:6: note: candidate: template<class T, int N> void getBoyAges(T (&)[N])
 void getBoyAges(T (&boy)[N]){
      ^~~~~~~~~~
stack.cpp:34:6: note:   template argument deduction/substitution failed:
stack.cpp:51:19: note:   variable-sized array type ‘long int’ is not a valid template argument
     getBoyAges(boy);

我的程序

#include <iostream>

class People{
    public:
        char *name;
        int age;
};

class Boy : public People{
    public:
        void say(void){
            std::cout << "i`m the most handsome!" << std::endl;
        }
};

template<class T, int N> 
void boyInitial(T (&boy)[N]){
    int i;
    for(i=0;i<N;i++){
        std::cout << "please input boy No." << i+1 << "`s age" << std::endl;
        std::cin >> boy[i].age; 
    }
}

template<class T, int N>
void getBoyAges(T (&boy)[N]){
    int i;
    for(i=0;i<N;i++){
        std::cout << boy.age << std::endl;
    }
}

int main(int argc, char **argv){
    using namespace std;
    int boyNumber = 0;
    cout << "how many boys do you want?" << endl;
    cin >> boyNumber;
    Boy boy[boyNumber];
    boyInitial(boy);
    getBoyAges(boy);
    return 0;
}

描述

我试图通过引用的方式将 Boy 类型的数据 boy 传递给函数 boyInitial() 和 getBoyAges(),然后我收到这样的错误。

我试图模仿这里的代码,然后得到错误。 Image

非常感谢您的帮助!

最佳答案

关键错误消息如下

note: variable-sized array type ‘long int’ is not a valid template argument

您声明了一个变长数组(数组的大小不是常量表达式)

int boyNumber = 0;
cout << "how many boys do you want?" << endl;
cin >> boyNumber;

Boy boy[boyNumber];

这不是标准 C++ 功能。

使用标准容器 std::vector 而不是可变长度数组。

最后一个程序之所以有效,是因为没有可变长度数组。数组的大小在编译时已知。

至少你可以声明像这样的函数

template<class T> 
void boyInitial(T *boy, size_t n ){
    for( size_t i=0; i < n; i++){
        std::cout << "please input boy No." << i+1 << "`s age" << std::endl;
        std::cin >> boy[i].age; 
    }
}

该函数可以这样调用

boyInitial( boy, boyNumber );

关于c++ - 没有匹配的函数可供调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68346409/

相关文章:

c++ - 将指针传递给导致第二次调用使程序崩溃的函数

c++ - if 中的 OR 语句是否只检查一个条件?平衡支架

c++ - C++ 中的模板和标准算法

coding-style - 干净代码中的降级规则

JavaScript 函数声明和求值顺序

c++ - 将迭代器传递给 lambda

c++ - Meson 将头文件复制/安装到输出目录并保留文件夹结构

c++ - 实现在模板类中定义的非模板方法

c++ - 当 foo 是接受单个模板参数的结构时, `foo<Type1, Types...>` 是否合法?

python - 带有*的Python函数作为参数:def blah(x,*,y)