Recommended Posts

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.

Share on other sites
SiCrane    11839
Use a function declaration:
void Function2();void Function1() {  Function2();}void Function2() {}

Share on other sites
Thanks. Worked like a charm.

Share on other sites
Promethium    580
Or even better, since your writing C++, use classes [smile]

Share on other sites
jyk    2094
Quote:
 Original post by PromethiumOr 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.