C++ 错误:体系结构 x86_64 的未定义符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22818925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ error: Undefined symbols for architecture x86_64
提问by Timur Ridjanovic
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]
, ....
我正在尝试学习 C++ 并试图解决一个问题,其中给定了许多步骤和您可以爬上台阶的可能方式的数量,并给出您可以爬上台阶的所有可能方式的排列。例如,如果要爬 5 个台阶,我可以一次向上移动 1 步、一次 2 步或一次 3 步,我需要打印出 1、2 和 3 的所有排列加起来 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;
}
回答by jhaight
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
您的编译器错误来自这样一个事实,即您的前向声明_num_steps
签名与您定义的签名不匹配_num_steps
。类型steps_list
不匹配
Change your prototype line to:
将您的原型线更改为:
void _num_steps(int amount, vector<int> possible_steps, vector<int> steps_list, vector<vector<int>> result);
回答by David Zech
The types in argument list of a function declaration and its definition must be the same.
函数声明的参数列表中的类型与其定义必须相同。
Yours don't match.
你的不匹配。
Declaration:
宣言:
void _num_steps(int amount, vector<int> possible_steps, vector<vector<int>> steps_list, vector<vector<int>> result);
Definition:
定义:
void _num_steps(int amount, vector<int> possible_steps, vector<int> steps_list, vector<vector<int>> result) { /* ... */ }