The Alien
The Alien

Reputation: 25

Using Undefined Functions in C++

How do I use an undefined function2 in my C++ program? The function2 is defined later, but I need to use the function2 in function1. However, function2 needs function1 to work, too! How do I do this?

function1(){...function2()...}
function2(){...function1()...}

Something like that.

Upvotes: 2

Views: 98

Answers (3)

R Sahu
R Sahu

Reputation: 206707

When you have functions calling each other, it's best to declare all the functions first. Then, the functions can be used in the implementation without any problem.

// Declarations
void function1();
void fucntion2();

// Implementations
void function1() { ... function2(); }

void function2() { ... function1(); }

Upvotes: 1

Chad
Chad

Reputation: 3018

Forward declare both functions.

void function1();
void function2();

function1(){...function2()...}
function2(){...function1()...}

Upvotes: 2

Potatoswatter
Potatoswatter

Reputation: 137890

Use a forward declaration:

void function2();

This specifies the interface of function2 (no return value, no parameters) so that function1 can call it.

Upvotes: 5

Related Questions