You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++调用make_connected函数出现"No matching function"错误求助

问题:编译报错"No matching function for call make_connected"

编译时main函数中所有标注ERROR的make_connected调用位置均报错:"No matching function for call make_connected",确认参数调用逻辑无误,怀疑数组传递方式存在问题。

错误原因分析

  1. 非标准变长数组(VLA)导致类型不匹配
    代码中使用了int degrees[n]、std::forward_list<int> connections[n]这类变长数组,这是GCC的扩展特性,并非标准C语法。而make_connected函数声明中使用的数组引用(如bool (&connected)[])属于不完整类型,标准C要求数组引用必须明确指定大小,但变长数组的大小是运行时确定的,编译器无法完成函数参数的类型匹配。

  2. 数组引用参数写法不符合标准
    标准C++中,无法直接声明大小未知的数组引用参数(bool (&connected)[]这种写法不合法),必须指定数组大小,或者改用容器类型传递。

解决方案:改用标准容器替代变长数组

将所有变长数组替换为std::vector,容器可以通过引用安全传递,且符合标准C++规范,同时解决类型匹配问题。

修改后的完整代码

#include <iostream>
#include <forward_list>
#include <vector>
#include <algorithm>

struct edge{
    int n1;
    int n2;
    edge(int p1, int p2) : n1(p1), n2(p2) {}
};

void make_connected(int i, std::vector<int>& degrees, std::vector<bool>& connected, std::vector<std::forward_list<int>>& connections, std::forward_list<int>& not_full, int k);

void find_not_full_neighbour(int& not_full_neighbour, std::forward_list<int>& connections, std::vector<int>& degrees, std::vector<bool>& connected, int k);

int main(){
    int n; //#nodes
    int k; //max #edges per node
    int p; //#edges

    std::cin >> n >> k >> p;

    std::vector<int> degrees(n, 0); //#edges per node,初始化为0
    std::vector<std::forward_list<int>> connections(n);
    std::vector<bool> connected(n, false);

    std::forward_list<int> not_full; //Nodes I can connect more edges to

    int to_add_count = 0;
    std::forward_list<edge> to_add; //Solution

    for(int i = 0; i < p; i++){ //Add edges from input
        int n1;
        int n2;
        std::cin >> n1 >> n2;
        if(n1 != n2){
            connections[n1].push_front(n2);
            connections[n2].push_front(n1);
        }
        degrees[n1]++;
        degrees[n2]++;
        not_full.push_front(n1);
        not_full.push_front(n2);
    }

    if(not_full.empty()){ //If there are no nodes with less than k edges
        for(int i = 0; i < n; i++){
            if(degrees[i] < k){ //If can connect more edges
                degrees[i]++; //Increase the degree
                not_full.push_front(i); //Add this node to the list
                make_connected(i, degrees, connected, connections, not_full, k);
            }
        }
    }
    
    for(int i = 0; i < n; i++){
        if(connected[i]){ //If already connected skip
            continue;
        }

        if(degrees[i] < k){ //If can connect more edges
            if(!not_full.empty() && degrees[not_full.front()] < k){ //If can connect to first node in list
                degrees[i]++; //Increase the degree
                degrees[not_full.front()]++; //Increase the degree of the node in the list

                to_add_count++;
                to_add.push_front(edge(i, not_full.front())); //Add to the solution

                connections[i].push_front(not_full.front()); //Add to the connections list

                not_full.push_front(i); //Add this node to the list

                make_connected(i, degrees, connected, connections, not_full, k);
            }
            else if(!not_full.empty() && degrees[not_full.front()] >= k){ //If can't connect to first node in list
                not_full.pop_front(); //Pop and run again
                i--; 
            }else if(not_full.empty()){ //If list is empty
                std::cout << -1 << std::endl; //No solution
                return 0;
            }
        }
        else{ //If can't connect more edges
            int not_full_neighbour = -1;
            find_not_full_neighbour(not_full_neighbour, connections[i], degrees, connected, k); //Find a not full neighbour
            if(not_full_neighbour == -1){
                std::cout << -1 << std::endl; //No solution
                return 0;
            }
            else{ //If there is a not full/connected neighbour
                degrees[not_full_neighbour]++; //Increase the degree
                degrees[not_full.front()]++; //Increase the degree of the node in the list

                to_add_count++;
                to_add.push_front(edge(not_full_neighbour, not_full.front())); //Add to the solution

                connections[not_full_neighbour].push_front(not_full.front()); //Add to the connections list

                not_full.push_front(not_full_neighbour); //Add this node to the list

                make_connected(i, degrees, connected, connections, not_full, k);
            }
        }
    }

    std::cout << to_add_count << std::endl;
    for(edge e : to_add){
        std::cout << e.n1 << " " << e.n2 << std::endl;
    }

    return 0;
}

void make_connected(int i, std::vector<int>& degrees, std::vector<bool>& connected, std::vector<std::forward_list<int>>& connections, std::forward_list<int>& not_full, int k){
    connected[i] = true;
    if(degrees[i] < k){
        not_full.push_front(i);
    }
    for(int n : connections[i]){
        make_connected(n, degrees, connected, connections, not_full, k);
    }
}

void find_not_full_neighbour(int& not_full_neighbour, std::forward_list<int>& connections, std::vector<int>& degrees, std::vector<bool>& connected, int k){
    for(int n : connections){
        if(degrees[n] < k && connected[n]){ //If found
            not_full_neighbour = n; //Set and return
            return;
        }
        else{ //If not found
            find_not_full_neighbour(not_full_neighbour, connections[n], degrees, connected, k); //Run on this node
        }
    }
}

关键修改点说明

  1. 把所有变长数组替换为std::vector:
    • int degrees[n] → std::vector<int> degrees(n, 0)(初始化所有元素为0,避免原代码中未初始化的问题)
    • std::forward_list<int> connections[n] → std::vector<std::forward_list<int>> connections(n)
    • bool connected[n] → std::vector<bool> connected(n, false)(替代原std::fill的初始化方式)
  2. 修改函数参数列表,将数组参数改为对应的vector引用,确保类型匹配;
  3. 修复find_not_full_neighbour函数中的递归调用错误:原代码递归时传递的是当前节点的邻接表,改为传递邻接节点的邻接表;
  4. 给edge结构体改用初始化列表构造,更符合C++规范。

内容的提问来源于stack exchange,提问作者Jan Hrubec

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 08:02:34