A question about functions

Started by
3 comments, last by Zakwayda 15 years, 6 months ago
I have a small question about c++ functions. When I recently started a large project, I noticed that whenever I wrote a function that referenced a function below it(physically in the code) it would throw an error. I think this is due to the compiler reading the code top to bottom. What can I do to work around this? See below for an example: (BTW my compiler is Visual C++ 2008 Express) Code: void Function1() { ... Function2(); } void Function2() { ... } End Code Thanks.
Advertisement
Use a function declaration:
void Function2();void Function1() {  Function2();}void Function2() {}
Thanks. Worked like a charm.
Or even better, since your writing C++, use classes [smile]
Quote:Original post by Promethium
Or even better, since your writing C++, use classes [smile]
Well, using classes wouldn't necessarily be 'better'; free functions have their place in C++, as does procedural programming in general. In any case, since we don't know what the OP is actually doing, we can't really say for certain that using classes would be a better choice.

Also, note that the issue in question applies to classes as well; the problem of declaration order is not specific to functions.

This topic is closed to new replies.

Advertisement