C ++错误:体系结构x86_64的未定义符号

问题描述 投票:7回答:2

我正在尝试学习C ++并试图解决一个问题,在这个步骤中给出了许多步骤以及可以采用的步骤的数量,给出了爬上台阶的可能方法的所有排列。因此,例如,如果有5个步骤可以攀爬,我可以一次向上移动1步,一次向上移动2步,或者一次向上移动3步,我需要打印出1,2和3的所有排列,加起来5:[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;
} 
c++ c++11 vector permutation
2个回答
10
投票

您的编译器错误来自于您的_num_steps正向声明的签名与_num_steps定义的签名不匹配。 steps_list的类型不匹配

将原型行更改为:

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

6
投票

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

你的不匹配。

宣言:

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) { /* ... */ }
© www.soinside.com 2019 - 2024. All rights reserved.