Reputation: 130
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
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
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
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
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
Reputation: 413
This will solve your problem:
int function2();
int function1() {
if (true) {
function2();
}
}
int function2() {
//do stuff
function1();
}
Upvotes: 1
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
Reputation: 487
Forward declare function2().
int function2(); //forward declaration
int function1() {
if (somethingtrue) {
function2();
}
}
int function2() {
//do stuff
function1();
}
Upvotes: 1
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