简体   繁体   English

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

[英]c++ - no matching function for call to

My terminal messages我的终端消息

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

My Program我的程序

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

Description描述

I'm trying to deliver Boy-type data boy, to the functions boyInitial() and getBoyAges(), by means of references, and then I get such errors.我试图通过引用将男孩类型的数据男孩传递给函数 boyInitial() 和 getBoyAges(),然后我得到了这样的错误。

I am trying to imitate the code here, and then get the errors.我试图模仿这里的代码,然后得到错误。 图像

I`d appreciate your help!我很感激你的帮助!

The key error message is the following关键错误信息如下

note: variable-sized array type 'long int' is not a valid template argument注意:可变大小的数组类型“long int”不是有效的模板参数

You declared a variable length array (the size of the array is not a constant expression)你声明了一个变长数组(数组的大小不是一个常量表达式)

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

Boy boy[boyNumber];

that is not a standard C++ feature.这不是标准的 C++ 功能。

Instead of variable length arrays use the standard container std::vector .使用标准容器std::vector代替可变长度数组。

The presented last program works because there is no variable length array.所提供的最后一个程序有效,因为没有可变长度数组。 The size of the array is known at the compile time.数组的大小在编译时是已知的。

At least you could declare the functions like for example至少你可以声明函数,例如

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

The function can be called like该函数可以像这样调用

boyInitial( boy, boyNumber );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM