C ++错误:体系结构x86_64的未定义符号 [英] C++ error: Undefined symbols for architecture x86_64
问题描述
我正在尝试学习C ++,并试图解决一个问题,其中给出了许多步骤以及可以逐步执行的所有可能方式,给出了可以逐步执行的所有可能方式的排列.因此,例如,如果要爬5步,并且我可以一次向上移动1步,一次向上2步或一次向上3步,则需要打印出所有1、2和3的排列总计5:[1, 1, 1, 1, 1]
,[1, 1, 1, 2]
,....
I'm trying to learn C++ and was trying to solve a problem where given a number of steps and the number of possible ways you can climb up the steps, give all the permutations of the possible ways you can climb up the steps. So for example, if there are 5 steps to climb and I can either move up 1 step at a time, 2 steps at a time or 3 steps at a time, I would need to print out all permutations of 1, 2 and 3 that add up to 5: [1, 1, 1, 1, 1]
, [1, 1, 1, 2]
, ....
我从此代码开始(尚未完成),但出现此错误:
I started with this code (it's not done yet), but I get this error:
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
我真的不明白我在做错什么.如果能得到帮助,我将不胜感激.谢谢!
I really don't get what I'm doing wrong. I'd appreciate it if I could get some help. Thanks!
#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
的类型不匹配
Your compiler error is coming from the fact that your signature for the forward declaration of _num_steps
does not match the signature of your definition of _num_steps
. the type of steps_list
does not match
将原型行更改为:
void _num_steps(int amount, vector<int> possible_steps, vector<int> steps_list, vector<vector<int>> result);
这篇关于C ++错误:体系结构x86_64的未定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!