22

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;
} 
brokenfoot
  • 11,083
  • 10
  • 59
  • 80
Timur Ridjanovic
  • 1,002
  • 1
  • 12
  • 18
  • 1
    Did you look at any of the other posts containing this exact error? While not all of the might be about your exact topic, they all contain an explanation of what causes the error and information about how to correct it, any of which can (with a minor amount of thinking) be applied to your own code here. If there are already several dozen posts asking the same thing and receiving the same answer, it seems pretty redundant to add yet another one. (See the **Related** list to the right of this question for at least 9 of those other links, for instance.) – Ken White Apr 02 '14 at 17:46

2 Answers2

24

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);
Azeem
  • 11,148
  • 4
  • 27
  • 40
jhaight
  • 299
  • 2
  • 4
12

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) { /* ... */ }
Azeem
  • 11,148
  • 4
  • 27
  • 40
David Zech
  • 745
  • 3
  • 14