user3736114 user3736114 - 1 month ago 4
C++ Question

Recursively add array numbers with inner class functions c++

Having some trouble figuring out what this error is.

#include <iostream>

class listSolver{
private:
int arraySize;
int *address;
public:
listSolver(int *arrayAddress, int size);
~listSolver();
int forLoopSolver();
int whileLoopSolver();
int recursiveSolver();
int addNumbers(int *numAddress,int num, int count);
};

listSolver::listSolver(int *arrayAddress,int size){
arraySize = size;
address = arrayAddress;
}

listSolver::~listSolver(){
}

int listSolver::recursiveSolver(){
int *funcAddress = address;
int size = arraySize;
int solution = 0;
return this->addNumbers(funcAddress,size,solution);
}
int addNumbers(int *numAddress,int num, int count){
if(count == 0){
return num+*numAddress;
}
else{
count--;
num+=*numAddress;
numAddress++;
return listSolver::addNumbers(numAddress,num,count);
}
}


g++ compiler gives me


/tmp/ccuy9bHM.o: In function 'listSolver::recursiveSolver()':
problem1.cpp:(.text+0x1e2): undefined reference to 'listSolver::addNumbers(int*, int, int)'
collect2: error: ld returned 1 exit status


Not sure what it means. Been trying different cominations of calling the function and can't get it to work.

Answer
int ListSolver::addNumbers(int *numAddress,int num, int count){
    ^^^^^^^^^^^^

Need the class name with the scope operator ("four dots" as we call it here for ages - TY panta rhei for the spot) to define a member function outside a class body. The way it is, you've defined a global function int addNames(/* ... */) that compiles successfully per se, but since you are trying to call the said member function, at link time, an error occurs.

Comments