0

So C++ won't let me use a function physically above where I declare it (in other words, the function must be on a smaller line number than its use). My problem is my functions all depend on at least one of the others. In other words:

void funct1()
{
     if (something is true)
     {
         funct2();
     }
     else
          cout << someResult;
}


void funct2()
{
     if(something is true)
     {
         funct3();
     }
     else
          cout << someResult;
}

void funct3()
{
     if (something is true)
     {
         funct1();
     }
     else
          cout << someResult;
}
}

In other words, each function needs to call one of the others in some cases. This will not work regardless of what order I put the functions in because at least one depends on something below it. How do I make the compiler look below the current function when compiling (i.e. read everything then decide what is valid) I am using g++ on CodeBlocks.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
  • If all functions depend on each other, that's usually an indication of a design flaw, but there are forward declarations. – chris Mar 25 '13 at 22:43
  • You need to forward declare funct1 and funct3 => http://www.learncpp.com/cpp-tutorial/17-forward-declarations/ – user2184879 Mar 25 '13 at 22:45
  • Also, look @ http://stackoverflow.com/questions/4757565/c-forward-declaration for a great overview. – user2184879 Mar 25 '13 at 22:51

2 Answers2

2

Add some forward declarations before the function definitions:

void funct1();
void funct2();
void funct3();

This way, you can use the functions in any order.

mfontanini
  • 21,410
  • 4
  • 65
  • 73
0

I'm presuming you meant for the last function to be funct3. You can get away with using a single forward declaration:

void funct2();

void funct1() { if(something is true) { funct2(); } else cout << someResult; }

void funct3() { if(something is true) { funct1(); } else cout << someResult; }

void funct2() { if(something is true) { funct3(); } else cout << someResult; }

It just completes the cycle. funct2 <- funct1 <- funct3 <- funct2.

However, I'm highly suspicious of any code that has dependencies like this.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • thanks, the reason for the dependencies is that I am creating a cascading loop of actions (one thing causes the next, some of which start another chain off) – user2209501 Mar 25 '13 at 23:10