Recursion in C++


I'm a java programmer and I really can't find my coder error.

I have a normal recursive method in my program:

1
2
3
4
5
6
7
8
9
10
11
int i = 0;

void Graph::calc(){

if(i>10){}
else{
    i++;
    calc();
}

}


where I declare i=0 in my constructor.

When I run my program, it just exits without any warning or error.

Any idea?
What exactly are you trying to do? Recursive functions usually have a parameter that is used to check the base case and initialize the first call. They typically add/subtract/multiply/divide by making a recursive/successive calls to the function.
This is my actual method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void SPBruteForce::getSPAll(Vertex *e, Cycle *c){
    Cycle *b = new Cycle(1024);
    for(int i = 0; i < c->getSize(); i++)
        b->addVertex(c->getVertex(i));
    if(c.getLast()->getName() == e->getName()){
        allCycles[currentCycles] = c;
        currentCycles++;
        QMessageBox msgBox;
        msgBox.setText("jkjh");
        msgBox.exec();
    }else{
        int f = c.getLast()->getNumberOfNeighbours();
        for(int i = 0; i < f; i++){
            if(!c.contains(c.getLast()->getNeighbour(i)->getName())){
                c.addVertex(c.getLast()->getNeighbour(i));
                getSPAll(e, c);
            }
        }
    }
}


But it also gave me an error, so I tried with the default recursion, but that didn't work either.
Is it a problem of stack overflow?
I have no idea, the program just exits
As I see your first sample is working. But I would like to do it by another way

int Graph::calc(int i)
{
if(i > 10)
{
return i;
}
else
{
return calc(++i);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void SPBruteForce::getSPAll(Vertex *e, Cycle *c){
    Cycle *b = new Cycle(1024);
    for(int i = 0; i < c->getSize(); i++)
        b->addVertex(c->getVertex(i));
    if(c.getLast()->getName() == e->getName()){
        allCycles[currentCycles] = c;
        currentCycles++;
        QMessageBox msgBox;
        msgBox.setText("jkjh");
        msgBox.exec();
    }else{
        int f = c.getLast()->getNumberOfNeighbours();
        for(int i = 0; i < f; i++){
            if(!c.contains(c.getLast()->getNeighbour(i)->getName())){
                c.addVertex(c.getLast()->getNeighbour(i));
                getSPAll(e, c);//Please make shure that you can execute this instruction
            }
        }
    }
}

See my comment
Topic archived. No new replies allowed.