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