Noah
Noah

Reputation: 130

C++ Call function that is not defined yet

I have this code. How do I do this without creating an error?

int function1() {
    if (somethingtrue) {
        function2();
    }
}

int function2() {
    //do stuff
    function1();
}

Upvotes: 5

Views: 3396

Answers (8)

LovelyHanibal
LovelyHanibal

Reputation: 395

The usual way to do this is to first declare your functions. The general convention is to put declaration in *.h file, and definition in *.cpp file. And then include *.h file in *.cpp file. That way you can use function1 and function2 in other files as well without repeating declaration. You just inlcude functions.h in that other file.

// functions.h
int function1();
int function2();

// functions.cpp
#include "functions.h"

int function1() {
    if (somethingtrue) {
        function2();
    }
}

int function2() {
    //do stuff
    function1();
}

However if you have a lot of functions and you want quick and dirty workaround you can do it like this:

class Root{public:

    int function1() {
        if (somethingtrue) {
            function2(); /// That will work now
        }
    }

    int function2() {
        //do stuff
        function1();
    }

    /// You can also put here variables, structs, classes, etc.
    /// and they will see each other regardless of order in code.

    /// But don't put main here
    /// it won't work

} root;

/// but now to call functoin1 or function2 from outside we need to use root
void main(){
    root.function1();
}

Root and root is just en example here. You can use any unused class name and variable name.

Upvotes: 0

NathanOliver
NathanOliver

Reputation: 180945

This is a case for a forward declaration. A forward declaration tells the compiler that the name is going to exist, what it's type is and allows you to use it in a limited context before you define it.

int function2();  // this lets the compiler know that that function is going to exist

int function1() {
    if (somethingtrue) {
        function2(); // now the compiler know what this is
    }
}

int function2() { // this tells the compiler what it has to do now when it runs function2()
    //do stuff
    function1();
}

Upvotes: 9

iksemyonov
iksemyonov

Reputation: 4196

int function2();

int function1() {
    if (somethingtrue) {
        function2();
    }
}

int function2() {
    //do stuff
    function1();
}

This is called forward declaration. You let the compiler know there is a function called function2(), without defining it. This is enough for the compiler to insert a call to that function when it's called inside function1(), which is then resolved at link-time.

Upvotes: 1

Entwicklerpages
Entwicklerpages

Reputation: 126

Just put this above your functions:

int function1();
int function2();

But don't create an endless loop! With that two lines you tell the compiler that function1 and function2 will be defined in the future. In bigger projects you would use header files. There you do the same but can use the functions in multiple files.

And also don't forget the return statement. I think your code example was only demonstration but I only want to mention it.

In C++ you must seperate declaration and definition. Read more about it here: https://stackoverflow.com/a/1410632/4175009

Upvotes: 2

firepower91
firepower91

Reputation: 413

This will solve your problem:

int function2();

int function1() {
    if (true) {
        function2();
    }
}

int function2() {
    //do stuff
    function1();
}

Upvotes: 1

maddin45
maddin45

Reputation: 747

Use forward declaration. You can declare a function before using it by just writing its signature, followed by a semicolon. With that, you promise the compiler that there will be a function definition with the same signature somewhere else.

int function2();

int function1() {
    if (somethingtrue) {
        function2();
    }
}

int function2() {
    //do stuff
    function1();
}

Upvotes: 3

Arne Böckmann
Arne Böckmann

Reputation: 487

Forward declare function2().

int function2(); //forward declaration

int function1() {
    if (somethingtrue) {
        function2();
    }
}

int function2() {
    //do stuff
    function1();
}

Upvotes: 1

xinaiz
xinaiz

Reputation: 7788

Declare second function before first, so it can see it's existence.

int function2();

int function1() {
    if (somethingtrue) {
        function2();
    }
}

int function2() {
    //do stuff
    function1(); //beware of double recursion.
}

Upvotes: 1

Related Questions