C++ - 没有匹配的函数调用

Zohar-fzh

我的终端消息

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;
}

描述

我试图通过引用将男孩类型的数据男孩传递给函数 boyInitial() 和 getBoyAges(),然后我得到了这样的错误。

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

我很感激你的帮助!

来自莫斯科的弗拉德

关键错误信息如下

注意:可变大小的数组类型“long int”不是有效的模板参数

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

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 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++:没有匹配的函数可调用''

C ++错误没有匹配的调用函数

C ++:没有匹配的函数来调用

C ++模板:没有匹配的调用函数

c ++:函数“没有匹配的函数可调用”错误

发生没有匹配的函数来调用C ++中的错误

在C ++映射中查找值:没有匹配的调用函数

C ++错误没有匹配函数调用静态模板方法

调用 CLASS:CLASSCPP(C++) 没有匹配的函数

C ++ Lambda-错误:没有匹配的函数可调用

C ++-没有匹配的函数来调用'getaddrinfo'

TCP-C ++-没有匹配的函数可以调用

C ++线程-没有匹配的函数来调用

重构C ++,“没有匹配的函数要调用”

C ++结构错误“没有匹配的调用函数...”

C ++错误::没有匹配的函数来调用'function'

线程C ++:没有用于调用的匹配函数

C ++中的QFileDialog:“没有匹配的调用函数”

C函数没有被调用?

构造函数中没有用于调用的匹配函数-C ++ 11

C ++:没有匹配的调用函数:为什么需要一个空的构造函数?

C ++中没有匹配的函数:将常量标记添加到调用函数

c ++构造函数没有匹配的函数

C ++错误:使用argc,* argv []和多维数组调用void函数时,“没有匹配的函数要调用”

使用Lambda时C ++没有匹配的函数调用进行转换

C++泛型方法给出“没有匹配的调用函数”错误

C ++错误:没有匹配的函数来调用'print_size'

如何在 C++ 中修复“没有匹配的函数调用...”?

C ++中的多线程处理时出现“没有匹配的函数调用”错误