C++ error: Undefined symbols for architecture x86_

2020-03-10 06:12发布

问题:

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;
} 

回答1:

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

Change your prototype line to:

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


回答2:

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) { /* ... */ }