I'm Creating a class that I want to call a recursive function within it. However, the complier says i cant declare a function within a method, I was wondering how I could Recursively call that function, in the method, or how to recursively call that method, with out declaring the function out of the class;
#include <iostream>
#include <vector>
#include <math.h>
#include <cstring>
using namespace std;
class RMatrix
{
int sizeH;
int sizeL;
int TotalN, Replace;
protected:
bool ** Matrix;
RMatrix(int, int);
~RMatrix(){for (int x = 0; x<sizeH; x++)delete [] Matrix[x]; delete [] Matrix;}
void DisplayMx ();
void RepMatrixf();
};
RMatrix::RMatrix(int TND, int NRep)
{
TotalN=sizeL=TND;
Replace=NRep;
int PrevResult = TND; // previous result will equal
for (int C = 1; C<NRep; C++) // iterates through each cell
{
PrevResult= (PrevResult*(TND-1))/(C+1); // changes previous result based on pascal formula
TND--; // subtracts from row
}
sizeH = PrevResult;
Matrix = new bool *[PrevResult]; // dynamic allocation array by pointer
for (int z = 0; z < PrevResult; z++)
{
Matrix[z] = new bool[TND]; // assign dynamically array
}
}
void RMatrix::RepMatrixf()
{
const int MAX = 100; // PROBLEM STARTS BELOW
void RepMatrix (int NDR, int size, int PosIR, int calls) // PosIR and calls initates at 0
{
if (calls==0)
memset(Matrix, 0, sizeof(Matrix)); // clears previous values of matrix **note memset only can set values for 0 or -1 in ints
static int Row = 0; // Row, stored for recursion
static int RepIndex[MAX]; // stores positions of variable to be turned true
for ( ; PosIR < size-(NDR-1); PosIR++) // Position index Replacement is less than number of digits being replaced
{ // Increase of each possible combination index
RepIndex[calls]=PosIR; // sets the replacement value for static array based of which call
if (NDR==1) // NDR is last digit
{
for (int x = 0; x<=calls; x++) // all possible replacement permutation are changed to false
{
Matrix[Row][RepIndex[x]]=true;
}
Row++; // row is increased
}
if (NDR>1) // recursively calls for ever digit in need of being replaced
{
RepMatrix((NDR-1), size, PosIR+1, calls+1);
}
}
return;
}
RepMatrix(Replace, TotalN, 0, 0);
}
void RMatrix::DisplayMx()
{
for (int x = 0; x < sizeH; x++)
{
for (int y = 0; y<sizeL; y++ )
{
cout << Matrix[x][y] << " ";
}
cout << endl;
}
}
int main ()
{
RMatrix T(5, 2);
T.DisplayMx();
return 0;
}