C++错误: 未定义符号,架构为x86_64。

22

我正在尝试学习C++,并尝试解决一个问题:给定一个台阶数和你可以爬上这些台阶的可能方法数,找出所有可能的爬楼梯方式。例如,如果有5个台阶要爬,并且我可以一次爬1步、2步或3步,我需要打印出所有能够相加得到5的1、2和3的排列组合:[1, 1, 1, 1, 1][1, 1, 1, 2],....

我开始用以下代码(未完成),但是我遇到了这个错误:

Undefined symbols for architecture x86_64:
  "_num_steps(int, std::__1::vector<int, std::__1::allocator<int> >, std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >, std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >)", referenced from:
      num_steps(int, std::__1::vector<int, std::__1::allocator<int> >) in num_steps-FTVSiK.o
ld: symbol(s) not found for architecture x86_64

我真的不明白我做错了什么。如果能得到一些帮助,我将不胜感激。谢谢!

#include <iostream>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

//prototypes
void _num_steps(int amount, vector<int> possible_steps, vector<vector<int>> steps_list,             vector<vector<int>> result);
int sum(vector<int> steps_list);
void num_steps(int amount, vector<int> possible_steps);
//
//
// 


void num_steps(int amount, vector<int> possible_steps) {
    vector<vector<int>> result;
    _num_steps(amount, possible_steps, {{}}, result);
    //print_result(result);
}


int sum(vector<int> steps_list) {
    int sum_of_steps(0);
    for (auto step: steps_list) {
        sum_of_steps += step;
    }
    return sum_of_steps;
}

void _num_steps(int amount, vector<int> possible_steps, vector<int> steps_list,  vector<vector<int>> result) {
    if (sum(steps_list) == amount) {
        result.push_back(steps_list);
        return;
    } 
    else if (sum(steps_list) >= amount) {
        return; 
    }
    for (auto steps: possible_steps) {
        auto steps_list_copy = steps_list;
        steps_list_copy.push_back(steps);
        _num_steps(amount, possible_steps, steps_list_copy, result);
    }
    cout << "yeah" << endl;
    return;
}


int main(int argc, char* argv[]) {
    num_steps(5, {1, 2, 3});
    return 0;
} 

1
你有看过其他包含这个错误的帖子吗?虽然它们不一定都是关于你的确切主题,但它们都包含了导致该错误的解释以及有关如何纠正它的信息,其中任何一个(稍加思考)都可以应用到你自己的代码中。如果已经有几十个帖子询问同样的问题并得到了相同的答案,那么再添加一个似乎就很多余了。(例如,请参见此问题右侧的“相关”列表中至少有9个链接。) - Ken White
2个回答

24

你的编译器错误是因为你前向声明 _num_steps 的签名与你定义 _num_steps 的签名不匹配,steps_list 的类型也不匹配。

请将原型行更改为:

void _num_steps(int amount, vector<int> possible_steps, vector<int> steps_list, vector<vector<int>> result);

12

函数声明中的参数类型和其定义中的必须相同。

你的参数类型不匹配。

声明:

void _num_steps(int amount, vector<int> possible_steps, vector<vector<int>> steps_list, vector<vector<int>> result);

定义:

void _num_steps(int amount, vector<int> possible_steps, vector<int> steps_list,  vector<vector<int>> result) { /* ... */ }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接