C++ 错误 : Undefined symbols for architecture x86_64

标签 c++ c++11 vector permutation

我正在尝试学习 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;
} 

最佳答案

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

将原型(prototype)行更改为:

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

关于C++ 错误 : Undefined symbols for architecture x86_64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22818925/

相关文章:

c++ - 使用哪个排序的 STL 容器来通过特殊键进行快速插入和查找?

c++ - 如何使用 cin 获取字符串而不是对其进行硬编码?

c++ - 为什么 'acquire/release'在c++11中不能保证顺序一致性?

c++ - 如何在 C++ 中从 "pointer-to-objects"的 vector 访问对象

c++ - 派生类中数据的设计和初始化

c++ - Qt 将代码从 Linux 迁移到 Windows

c++ - OpenCV3.0.0dev中鱼眼相机模型的主要引用有哪些?

c++ - 原子线程计数器

c++ - 使用 STL/Boost 查找和修改 vector 中的匹配元素

c++ - 创建分类音乐库的有效方法