You are on page 1of 24

2/16/13

1.7 Forward declarations Learn C++

1.6 Whitespace and basic formatting

1.8 Programs with multiple files

1.7 Forward declarations


BY A LEX, ON JUNE 2ND, 2007

Take a look at this seemingly innocent sample program called add.cpp: 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 # i n c l u d e< i o s t r e a m > i n tm a i n ( ) { u s i n gn a m e s p a c es t d ; c o u t< <" T h es u mo f3a n d4i s :"< <a d d ( 3 ,4 )< <e n d l ; r e t u r n0 ; } i n ta d d ( i n tx ,i n ty ) { r e t u r nx+y ; }

You would expect this program to produce the result: T h es u mo f3a n d4i s :7

But in fact, it doesnt compile at all! Visual Studio 2005 Express produces the following compile errors:

a d d . c p p ( 1 0 ):e r r o rC 3 8 6 1 :' a d d ' :i d e n t i f i e rn o tf o u n d a d d . c p p ( 1 5 ):e r r o rC 2 3 6 5 :' a d d ':r e d e f i n i t i o n ;p r e v i o u sd e f i n i t i o nw a s' f o r m e r l yu n k n o

The reason this program doesnt compile is because the compiler reads files sequentially. When it reaches the function call to add() inside of main(), it doesnt know what add is, because we havent defined add() until later! That produces the error on line 10. Then when it gets to the actual declaration of add(), it complains about add being redefined (which seems slightly misleading, given that it wasnt ever defined in the first place). Often times, a single error in your code will end up producing multiple warnings. Rule: When addressing compile errors in your programs, always resolve the first error produced first. In this case, that means we need to address the fact that the compiler doesnt know what add is. There are three ways to fix this problem. The first way is to reorder our function calls so add is defined before main: 1 2 3 4 5 6 7 # i n c l u d e< i o s t r e a m > i n ta d d ( i n tx ,i n ty ) { r e t u r nx+y ; }
1/24

www.learncpp.com/cpp-tutorial/17-forward-declarations/

2/16/13

1.7 Forward declarations Learn C++

8 9 1 0 1 1 1 2 1 3

i n tm a i n ( ) { u s i n gn a m e s p a c es t d ; c o u t< <" T h es u mo f3a n d4i s :"< <a d d ( 3 ,4 )< <e n d l ; r e t u r n0 ; }

That way, by the time main() calls add(), it will already know what add is. Because this is such a simple program, this change is relatively easy to do. However, in a large program, it would be extremely tedious trying to decipher which functions called which other functions so they could be declared in the correct order. Furthermore, this option is not always available. Lets say were writing a program that has two functions A and B. If function A calls function B, and function B calls function A, then theres no way to order the functions in a way that they will both be happy. If you define A first, the compiler will complain it doesnt know what B is. If you define B first, the compiler will complain that it doesnt know what A is. Consequently, a better solution is to use a forward declaration. In a forward declaration, we declare (but do not define) our function in advance of where we use it, typically at the top of the file. This way, the compiler will understand what our function looks like when it encounters a call to it later. We do this by writing a declaration statement known as a function prototype. A function prototype is a declaration of a function that includes the functions name, parameters, and return type, but does not implement the function. Heres our original program with a forward declaration: 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 # i n c l u d e< i o s t r e a m > i n ta d d ( i n tx ,i n ty ) ;/ /f o r w a r dd e c l a r a t i o no fa d d ( )u s i n gaf u n c t i o np r o t o t y p e i n tm a i n ( ) { u s i n gn a m e s p a c es t d ; c o u t< <" T h es u mo f3a n d4i s :"< <a d d ( 3 ,4 )< <e n d l ; r e t u r n0 ; } i n ta d d ( i n tx ,i n ty ) { r e t u r nx+y ; }

Now when the compiler reaches add() in main, it will know what add looks like (a function that takes two integer parameters and returns an integer), and it wont complain. It is worth noting that function prototypes do not need to specify the names of the parameters. In the above code, you could also forward declare your function like this: 1 i n ta d d ( i n t ,i n t ) ;

However, we prefer the method where the parameters are named because its more descriptive to a human reader. One question many new programmers have is: what happens if we forward declare a function but do not define it? The answer is: it depends. If a forward declaration is made, but the function is never called, the program will compile and run fine. However, if a forward declaration is made, the function is called, but the program never defines the function, the program will compile okay, but the linker will complain that it cant resolve the function call. Consider the following program:
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 2/24

2/16/13

1.7 Forward declarations Learn C++

1 2 3 4 5 6 7 8 9 1 0 1 1

# i n c l u d e" s t d a f x . h " # i n c l u d e< i o s t r e a m > i n ta d d ( i n tx ,i n ty ) ; i n tm a i n ( ) { u s i n gn a m e s p a c es t d ; c o u t< <" T h es u mo f3a n d4i s :"< <a d d ( 3 ,4 )< <e n d l ; r e t u r n0 ; }

In this program, we forward declare add(), and we call add(), but we never define add(). When we try and compile this program, Visual Studio 2005 Express produces the following message:

C o m p i l i n g . . . a d d . c p p L i n k i n g . . . a d d . o b j:e r r o rL N K 2 0 0 1 :u n r e s o l v e de x t e r n a ls y m b o l" i n t_ _ c d e c la d d ( i n t , i n t ) "( ? a d d @ @ Y A a d d . e x e:f a t a le r r o rL N K 1 1 2 0 :1u n r e s o l v e de x t e r n a l s

As you can see, the program compiled okay, but it failed at the link stage because int add(int, int) was never defined. The third solution is to use a header file, which we will discuss shortly. Quiz 1) Whats the difference between a function prototype and a forward declaration? 2) Write the function prototype for this function: 1 2 3 4 i n tD o M a t h ( i n tf i r s t ,i n ts e c o n d ,i n tt h i r d ,i n tf o u r t h ) { r e t u r nf i r s t+s e c o n d*t h i r d/f o u r t h ; }

For each of the following programs, state whether they fail to compile, fail to link, or compile and link. If you are not sure, try compiling them! 3) 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 # i n c l u d e< i o s t r e a m > i n ta d d ( i n tx ,i n ty ) ; i n tm a i n ( ) { u s i n gn a m e s p a c es t d ; c o u t< <" 3+4+5="< <a d d ( 3 ,4 ,5 )< <e n d l ; r e t u r n0 ; } i n ta d d ( i n tx ,i n ty ) { r e t u r nx+y ; }

www.learncpp.com/cpp-tutorial/17-forward-declarations/

3/24

2/16/13

1.7 Forward declarations Learn C++

4) 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 5) 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 6) 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 # i n c l u d e< i o s t r e a m > i n ta d d ( i n tx ,i n ty ,i n tz ) ; i n tm a i n ( ) { u s i n gn a m e s p a c es t d ; c o u t< <" 3+4+5="< <a d d ( 3 ,4 ,5 )< <e n d l ; r e t u r n0 ; } i n ta d d ( i n tx ,i n ty ,i n tz ) { r e t u r nx+y+z ; } # i n c l u d e< i o s t r e a m > i n ta d d ( i n tx ,i n ty ) ; i n tm a i n ( ) { u s i n gn a m e s p a c es t d ; c o u t< <" 3+4+5="< <a d d ( 3 ,4 )< <e n d l ; r e t u r n0 ; } i n ta d d ( i n tx ,i n ty ,i n tz ) { r e t u r nx+y+z ; } # i n c l u d e< i o s t r e a m > i n ta d d ( i n tx ,i n ty ) ; i n tm a i n ( ) { u s i n gn a m e s p a c es t d ; c o u t< <" 3+4+5="< <a d d ( 3 ,4 ,5 )< <e n d l ; r e t u r n0 ; } i n ta d d ( i n tx ,i n ty ,i n tz ) { r e t u r nx+y+z ; }

Quiz Answers 1) Show Solution 2) Show Solution 3) Show Solution 4) Show Solution 5) Show Solution 6) Show Solution
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 4/24

2/16/13

1.7 Forward declarations Learn C++

1.8 Programs with multiple files

Index

1.6 Whitespace and basic formatting

C ++ TU TOR IAL |

PR IN T TH IS POST

1.6 Whitespace and basic formatting

1.8 Programs with multiple files

70 comments to 1.7 Forward declarations


Learn C++ - 7.1 -- Function parameters and arguments
September 11, 2007 at 9:09 am Log in to Reply [...] Forward declarations [...]

beatavenger
November 1, 2011 at 4:42 pm Log in to Reply xcode doesnt understand add() Help!

GovZ
December 13, 2007 at 6:44 pm Log in to Reply Hello guys, You have a great tutorial here. Worthy of publication, IMHO. Anyways one question. Does Forward Declaration by function prototyping have an effect on the execution time or is this only used during the creation of some hash table or something? And that in the resulting executable, this step is not actually re-read. I hope I make sense. Thanks for your answers in advance. =)

Alex
December 13, 2007 at 8:08 pm Log in to Reply GovZ,
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 5/24

2/16/13

1.7 Forward declarations Learn C++

Forward declarations are used only to tell the compiler about the existence of a function (or class or variable) before it is actually implemented. This information is only used during compile time. Consequently, forward declarations will not make your executables larger or slower.

josh
January 28, 2008 at 11:34 am Log in to Reply rying out your second example i wrote: #include int add(int x+int y) { return x+y; } int main() { using namespace std; cout

Alex
January 28, 2008 at 11:49 am Log in to Reply Please repost your question with the code embedded inside PRE html tags.

WordPress
June 19, 2011 at 10:45 pm Log in to Reply If I am right the arguments should not be divided by a positive or plus symbol but rather by a comma.

Dan
February 1, 2008 at 4:25 pm Log in to Reply Ahh, ok Im getting a clearer picture of this now (re: my question a few days ago in the previous functions section when I asked about the order of functions in Cpp code). This helps explain the reasons why it must be. :) Thanks Alex! -Dan

WordPress
June 19, 2011 at 10:47 pm Log in to Reply Yeah we had this problem back there too! :D

www.learncpp.com/cpp-tutorial/17-forward-declarations/

6/24

2/16/13

1.7 Forward declarations Learn C++

Gary
March 22, 2008 at 7:46 pm Log in to Reply Hi there. I was wondering how come I have to type #include stdafx.h now. I havent used my compiler for a while, so I was just wondering. I cant do this program unless I do it, but the Hello World one works just fine without it. By the way, in my older programs, the source files say main.cpp but the new one has 2 files, one that says stdfax.cpp and another one with the title of my project. Im new to programming, so if my questions sound dumb, bear with me.

Alex
March 24, 2008 at 2:18 pm Log in to Reply stdafx.cpp is a file that Microsoft compilers use to do precompiled headers (which makes you program compile faster), if you sent them up correctly. If you dont want to deal with stdafx.h, you can always turn precompiled headers off (in the project settings).

Learn C++ - 1.8 Programs with multiple files


April 22, 2008 at 5:39 pm Log in to Reply [...] 2007 Prev/Next Posts 1.7 Forward declarations | Home | 1.9 Header files Saturday, June 2nd, 2007 at 8:26 [...]

Mitul
June 16, 2008 at 9:40 pm Log in to Reply /* I have writen one another Program like example 5*/ #include <iostream.h> #include <conio.h> int add(int x, int y, int z); void main() { int x,y,z; clrscr(); cout << "n Enter three number : n"; cin >> x; cin >> y; cin >> z; cout << "n" << x << " + " << y << " + " << z << " = " << add(x, y, z) << endl; getch(); } int add(int x, int y, int z) { return x + y + z; }
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 7/24

2/16/13

1.7 Forward declarations Learn C++

Schmeckle
September 26, 2008 at 7:50 am Log in to Reply Doesnt main have to be: i n tm a i n ( )

not v o i dm a i n ( )

so that main returns a value?

Alex
October 4, 2008 at 9:14 am Log in to Reply Many (most?) compilers let you get away with using void main() instead of int main(), and they will implicitly return 0 when you do this. However, its not technically part of the language, so I avoid it in my examples.

ice
September 7, 2010 at 1:43 pm Log in to Reply I tried to run your program and came up with and error like this 1> Build started: Project: 1, Configuration: Debug Win32 1> 1.cpp 1>c:\users\marius\documents\visual studio 2010\projects\1\1\1.cpp(2): fatal error C1083: Cannot open include file: iostream.h: No such file or directory ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Joyel
June 27, 2008 at 9:23 pm Log in to Reply This helped explain a lot of things I was previously having problems with.

jeff
December 7, 2008 at 7:23 am Log in to Reply i am experimenting the code from the quiz number 6 and i try to changed the forward declaration variables from i n ta d d ( i n tx ,i n ty ,i n tz ) ;

to
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 8/24

2/16/13

1.7 Forward declarations Learn C++

i n ta d d ( i n ta ,i n tb ,i n tc ) ;

after that i compiled the code by using microsoft visual studio 2008 and i notice that the code is successfuly compiled and the program is smoothly running. why is it that the compiler is not complaining while the forward declaration variable is different from the implementation?

Alex
December 7, 2008 at 11:01 am Log in to Reply Good question! It turns out that the variable names in your forward declarations dont have to match those in the actual definitions. This is because the variable names in the forward declarations simply arent used only the types are. You can even omit the variable names from the forward declaration altogether if you want. However, I think its generally good practice to put them in. If you see a forward declaration like this: v o i dd o S o m e t h i n g ( b o o l ) ;/ /w h a td o e st h i sb o o lm e a n ?

Its a lot less meaningful than if you see this: v o i dd o S o m e t h i n g ( b o o lb S i l e n t M o d e ) ;/ /n o ww eh a v ea ni d e a

jeff
December 8, 2008 at 2:45 am Log in to Reply now its clear and i understand well. thank you very much!

vkakwani
May 4, 2011 at 10:09 am Log in to Reply hi alex, first of all thnks a lot for such nice tutorials. i know its little too late to ask questions {looking at the postig dates} however i have one doubt. forward declaration in case of classes. can i have forward declaration of a class [class A] in .cpp file. this class definition is present in other header fil [A.h]. now, in my code i can have a class [class B] containing pointer to this class and forward declaration [class A;]. this will get compiled and linked. But if i create an object of class A in class B then i have to do #include A.h also. Any specific reason for this, my thinking is that compiler would require complete definition of object getting created inside class B, whereas in case of pointer, well.. its just pointer.

www.learncpp.com/cpp-tutorial/17-forward-declarations/

9/24

2/16/13

1.7 Forward declarations Learn C++

m i thinking correct?

adam
December 17, 2008 at 4:44 pm Log in to Reply So a function prototype is a kind of forward declaration?

Alex
December 22, 2008 at 11:29 pm Log in to Reply Its more accurate to say that one use of function prototypes is for forward declarations. Function prototypes can also be used in header files and in class declarations (covered in chapter 8). So function prototypes have more uses than just as forward declarations.

adam
December 18, 2008 at 6:43 pm Log in to Reply in solution one it says eturn instead of return [ Fixed! Thanks. -Alex ]

peddi
January 2, 2009 at 1:05 am Log in to Reply Can we say the forward declaration in nothing but declaring the function globally, where as function prototyping is declaring function locally i.e, in the main function? is that only the difference? If then there is no way we can block the outsider from accessing our function which declared using forward declaration?

Alex
January 3, 2009 at 6:56 pm Log in to Reply A forward declaration is a specific type of function prototype that allows you to declare a function before you actually define it. Unfortunately, theres no way to hide such functions from outsiders even if you dont declare a function prototype, someone who wanted to use your function could write a function prototype for it and then use it. The only way to hide functions is to put them inside classes, which well cover much later in this tutorial.

Julian
January 8, 2009 at 1:50 am Log in to Reply I just found this site today after frustratingly leaving a different learn C++ website, and I must say, your tutorials are a 100 times better than the other site I was on! :P It had about one paragraph on functions which was badly worded, and then went on to something about
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 10/24

2/16/13

1.7 Forward declarations Learn C++

arrays and loops and binary trees and it had examples but they used functions in a way I didnt even know was possible and it just confused the crackers outta me. I like the way these tutorials dont teach everything about functions in the one chapter, they teach the aspects of functions that would be easy to understand at this point, and then explain the more advanced features when the reader has a bit more knowledge about C++. Thankyou! :D

sara
February 24, 2009 at 6:43 am Log in to Reply i m very weak in programing how can i improve it

Alex
March 1, 2009 at 12:01 pm Log in to Reply Practice makes perfect! Pick a small project and figure out how to code it. Eg. a calculator, an address book, a little game, etc You will learn as much from working on your own code and solving the problems you run into as you will from these tutorials.

csvan
April 23, 2009 at 6:33 am Log in to Reply I double that. And triple it. I believe practical work is absolutely essential to building solid programming competence. You can learn all your life, but learning is in vain if it is only forgotten. Putting learnt knowledge into practice helps solidify it, and, I believe, can really contribute to it becoming a permanent part of your routine, not easily forgotten.

Justin
February 27, 2009 at 12:16 pm Log in to Reply Hi i have a question i wrote my own code (same as yours but didnt copy and paste) and i got a weird error that i do not under stand here is my code int add(int x,int y); { return x + y; } int main() { using namespace std; cout << the sum of 3+4 is : << add(3, 4)<< endl; system (pause); return 0; } and this is the error message: 3 D:Dev-Cppmain.cpp expected `, or `; before { token
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 11/24

2/16/13

1.7 Forward declarations Learn C++

Thankz Justin

Alex
March 1, 2009 at 12:16 pm Log in to Reply You have a semicolon after the line declaring add: i n ta d d ( i n tx , i n ty ) ;

should be: i n ta d d ( i n tx , i n ty )

Danny
March 10, 2009 at 2:09 pm Log in to Reply

# i n c l u d e s t d a f x . h # i n c l u d e i n ta d d ( i n t1 ,i n t2 ,i n t3 ,i n t4 ) ; i n tm a i n ( ) { u s i n gn a m e s p a c es t d ; c o u t< < T h es u mo ff i r s t ,s e c o n d ,t h i r d ,a n df o r t h= < <a d d ( 1 , 2 , 3 , 4 )< <e n d l ; r e t u r n0 ; } i n ta d d ( i n t1 ,i n t2 ,i n t3 ,i n t4 ) ; { r e t u r n1+2+3+4 ; }

Y doesn't it work? =/ can someone please help

Furious Sideburns
March 25, 2009 at 2:41 pm Log in to Reply Ill have a crack at answering this (Im new to all this myself). If Im understanding this right, youve got a few syntax errors. Youre missing a at the end of your first #include # i n c l u d e" s t d a f x . h
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 12/24

2/16/13

1.7 Forward declarations Learn C++

should be # i n c l u d e" s t d a f x . h "

Also, you need to lose the ; on the end of your forward declaration and your add function i n ta d d ( i n t1 ,i n t2 ,i n t3 ,i n t4 ) ;

and i n ta d d ( i n t1 ,i n t2 ,i n t3 i n t4 ) ;

should be i n ta d d ( i n t1 ,i n t2 ,i n t3 ,i n t4 )

and i n ta d d ( i n t1 ,i n t,i n t3 ,i n t4 )

csvan
April 23, 2009 at 6:25 am Log in to Reply You almost got it, however you NEED to have a ; at the end of forware declarations, since they are statements. Thus, it is compleely correct to write: i n ta d d ( i n ta ,i n tb ) ;

but not without the semicolon. C++ statements are ALWAYS concluded with semicolon (except for some structures, such as while and for loops). Also, you CANNOT write a parameters name as a number (for example int add(int 1);), that is not allowed by the compiler. Keep on coding. Use it for good. :)

csvan
April 23, 2009 at 6:31 am Log in to Reply You have 3 errors here: 1. The first line is missing a closing quotation mark () at the end (it should be stdafx.h). 2. The second include statement does not include anything. I think you meant to include iostream,
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 13/24

2/16/13

1.7 Forward declarations Learn C++

therefore it should read: # i n c l u d e< i o s t r e a m >

3. The name of a parameter (or any variable as far as I know) can never start with a number, or simply be a number. Therefore, you will have to rename the parameters both in the function prototype and definition for add, for example like this: i n ta d d ( i n ta ,i n tb ,i n tc ,i n t4 ) ;

and make the same changes to the body of the function (change 1 to a etc). Keep coding. Use it for good.

Rinummannobby
May 25, 2009 at 10:56 am Log in to Reply secure virginia slims ultra sunlight cigarettes second-rate doral cigarette cheaply doral cigarette|penurious doral cigarettes shoddy doral cigarettes cheap doral cigarettes inferior gpc cigarette tuppenny gpc cigarettes tuppenny gpc cigarettes cheap gpc cigarettes discount doral cigarettes

=o_O=
August 5, 2009 at 4:07 am Log in to Reply Well lookie lookie! SPAM!! GTFO!!!!!

Prayrit
July 17, 2009 at 12:06 am Log in to Reply This is a weird concept to me, because every other language Ive worked with(java, c#, actionscript) doesnt care where the function is located

StelStav
September 2, 2010 at 9:49 am Log in to Reply moved my question at the bottom of the queue

Jon
September 8, 2009 at 1:55 am Log in to Reply I dont know if this was mentioned yet, on a brief scan I didnt see it, so forgive me if you already answered this
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 14/24

2/16/13

1.7 Forward declarations Learn C++

You defined a function prototype as: A function prototype is a declaration of a function that includes the functions name, parameters, and return type, but does not implement the function. In Question #2 on this page you give us: 1 . i n tD o M a t h ( i n tf i r s t ,i n ts e c o n d ,i n tt h i r d ,i n tf o u r t h ) 2 . { 3 . r e t u r nf i r s t+s e c o n d*t h i r d/f o u r t h ; 4 . }

Did you mean us to solve for the forward declaration? If not, and you did mean to ask for the Function Prototype, shouldnt you have given us only the Forward Declaration?

Florian
November 28, 2009 at 4:09 pm Log in to Reply Hi, i do have a problem understanding this: # i n c l u d e< i o s t r e a m > i n ta d d ( i n tx ,i n ty ) ;/ /f o r w a r dd e c l a r a t i o no fa d d ( )u s i n gaf u n c t i o np r o t o t y p e i n tm a i n ( ) { u s i n gn a m e s p a c es t d ; c o u t< <" T h es u mo f3a n d4i s :"< <a d d ( 3 ,4 )< <e n d l ; r e t u r n0 ; } i n ta d d ( i n tx ,i n ty ) { r e t u r nx+y ; }

As the tutorial has stated earlier the program is running line by line so the function add would be unrecognized when it is called while add() is declared later on. On this example I would actually realize that we have created add() called it in main BUT we never reach the line where return x+y was written down. Basically I would expect a link error since the call ends up without returning anything? So my question is why does the program knows that add() returns the value x+y when it shouldnt reach that declaration? Hope someone can understand this, i am German my English certainly lacks a bit. Thanks Florian
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 15/24

2/16/13

1.7 Forward declarations Learn C++

Florian
November 28, 2009 at 4:17 pm Log in to Reply Ok, I was thinking about it for a second, here is my explanation maybe someone can tell me if i am right. The program does work because c++ is compiled rather than interpreted. Which means as soon as we have created a prototyp the compiler gets through until the end and the compiled program has overwriten the first Prototype with the completed declaration (since the later declaration supersedes the first statement? Florian

Eugene
November 28, 2009 at 11:35 pm Log in to Reply Not quite. The prototype does not get overwritten. Rather, the forward declaration informs the compiler of how a call of that function should look like, and thus the compiler can do the necessary setup and translate the function call into assembly/machine code. The function definition defines the code that will be executed when the function is called. In this case it is in the same source file as the function call, but it could be in a different source file, or might not even be available at all, e.g., just the object code is available in a shared library. That said, some functions whose function definitions are available might be inlined, i.e., the function call is replaced by the code of the function itself.

Michael O.
January 10, 2010 at 3:08 am Log in to Reply Hi Alex! Thanks a lot for these great tutorials, theyve teached me a lot (I began 2 days ago). I agree with comment #1, these tutorials deserve a publication! I have tried to learn C++ some times before, but all the books I ended up with were hard to understand, the language was difficult, but these tutorials are very easy to understand, so again, thanks a lot Alex! -Michael O.

Jeno
February 1, 2010 at 8:58 am Log in to Reply Hi, Thanks for this wonderful website!!! I wrote the following code, it compiles, links and executes well, but it seems like the Return Value is wrong:

/ /P r e p r o c e s s o rs t a t e m e n t
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 16/24

2/16/13

1.7 Forward declarations Learn C++

# i n c l u d e< i o s t r e a m > / / " G e t e m "F o r w a r dD e c l a r a t i o n i n tG e t e m ( i n to n e ,i n tt w o ,i n tt h r e e ,i n tf o u r ) ; / / " M a i n "f u n c t i o nd e c l a r a t i o n i n tm a i n ( ) { u s i n gn a m e s p a c es t d ;{ s t d : : c o u t < <G e t e m ( 7 ,8 ,3 ,7 ) < < e n d l ; } r e t u r n0 ; } i n tG e t e m ( i n to n e ,i n tt w o ,i n tt h r e e ,i n tf o u r ) { r e t u r no n e+t w o-t h r e e*f o u r ; }

The output return value I get is -6, but when I compute the numbers myself it is a wrong calculation. What Am I missing?

Michael B
February 4, 2010 at 5:11 pm Log in to Reply Order of Operations. PEMDAS is it?

Ireul
March 12, 2010 at 4:16 am Log in to Reply Sorry for disrespectful comment, but you miss grade school math classes. 7 + 8 3 * 7 = 15 21 = -6, the answer given by output is perfectly correct.

Liquidcool
December 7, 2010 at 8:58 pm Log in to Reply Rule 1: First perform any calculations inside parentheses. Rule 2: Next perform all multiplications and divisions, working from left to right. Rule 3: Lastly, perform all additions and subtractions, working from left to right.

zaykomoyot
February 13, 2010 at 9:19 am Log in to Reply [url=http://drugpills.net/cialis.html][size=20][color=red][b]BILLIG CIALIS KAUFEN ONLINE[/b][/color][/size]
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 17/24

2/16/13

1.7 Forward declarations Learn C++

[/url] [url=http://drugpills.net/cialis.html][img]http://drugpills.net/img/cialis1.gif[/img][/url] [url=http://drugpills.net/cialis.html][size=20][b][color=blue]BILLIG CIALIS KAUFEN[/color] [color=red]*** Jetzt Kaufen! ***[/color] [color=green]CIALIS KAUFEN ONLINE[/color][/b][/size][/url] [url=http://drugpills.net/cialis.html]Cialis Tadalafil Aus Indien [/url] [url=http://drugpills.net/cialis.html]Erektile Dysfunktion Cialis [/url] [url=http://drugpills.net/cialis.html]Besser Cialis Levitra Viagra, Die [/url] [url=http://drugpills.net/cialis.html]Kanada Cialis Online [/url] [url=http://drugpills.net/cialis.html]Cialis Dosis [/url] Cialis Gnc Cialic Bestellen Rabatt Cialis Levitra Viagra Cialis Soft Naturlichen Cialis Cialis In Hohen Blutdruck Pressur Cialis Droge Impotenz Cialis Und Taub Bein Wie Ist Cialis Zur Behandlung Von Herz-Probleme Bei Frauen Viagra Cialis Und Drogen Ist Die Bestellung Cialis Rechtlichen Apotheken On Line Cialis Billig Europaischen Cialis Cialic Kaufen Viagra Finden Suche 76k Cialis Websites Online Kostenlos Charles Fakten Uber Cialis Cialis Jeden Tag Cialis Und Diabetes Cialis Dreampharmaceuticals Cialis Gelee [i]Finden Cialis Tadalafil Cialis Oder Cialis Sales Uk Verschreibungspflichtige Medikamente Viagra Cialis Propecia Mann Gesundheit Cialis Tijuana Generika Cialis Nexium Rabatt Lilie Lcos Cialis Cialis Kanada Rx Cialis Frauen Cialis Rx Cialis Online-Verkauf Indien Cialis
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 18/24

2/16/13

1.7 Forward declarations Learn C++

Rezept Fur Cialis Kaufen Kaufen Viagra Cialis Levitra Uprima Cialis Viagra Besten Preis Auf Cialis Medikamente Wie Viargra Und Cialis Viagra Cialis Vs Foren Ubernachtung Lieferung Cialis Frei Cialis Ohne Rezept [/i] [u]Tadalis Edegra Generic Cialis Kauf Cialis Online Sie Cialis In Einer Apotheke Kapsel Cialis Kaufen Preiswerten Cialic Viagra Cialis Vs Online Cialis Traum Pharma Kaufen Besten K Cialis Gujpkr Surfen Zu Kaufen Cialis Online-Link Cialis Levitra Oder Cialis V S Viagra Softtabs Cialis Cialis 20 Generic Cialis Uberprufung Wirkstoff Cialis Wie Kommen Sie An Kostenlose Proben Von Cialis Cialis Rechtsanwalt Caverta Cialis Billig Cialis Maximalen Dosis Schlagworter Avodart Cialis Clomid Diflucan Dostinex Glucophage [/u] [b]Online-Rezept Fur Cialis Generic Cialis Tadalafil Niedrigsten Preise Cialis Cialis Patch Cialic Ohne Rezept Bestellen Cialis Erektion Gesundheit Mann Penis Viagra Apotheke Cialis Silagra Cumwithuscom Cialis Schuldenkonsolidierung Viagra Edinburgh Seiten Suche Finden Kaufen Cialis Avodart Cialis Clomid Diflucan Dostinex Gluco Xanax Viagra Cialis Porn Rolex Gru?E Sturm Beta-Jahre Cialis Cialis Forum Apotheke Billige Marke Cialis Cialis Ambien Cialis, Wo Flussigkeit Cialis Ordnungsgema?E Dosges Cialis Und Viagra Forum Keine Verschreibung Notwendig Cialis Kostenlos Viagra Cialis Levitra Prozess Bietet Cialis Genuinerx Net Viagra Viagra Viagra
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 19/24

2/16/13

1.7 Forward declarations Learn C++

[/b]

Canon
March 31, 2010 at 12:14 pm Log in to Reply This is lone of the pernicious effects of affirmative action. In our PC/affirmative effectiveness enlightenment, we nurse to spy the achievements and promotions of minorities as a consequence of affirmative affray policies in lieu of of distinct diligence and talent. This undermines these remarkably achievements. Another ungovernable here is that it sets human morality at odds with the higher heavenly morality. If the higher decency is common to adjudicator the Mass murder, necessary disease outbreaks, etc, as effects, while forgiving propriety will judge them miasmic, then the proficient in each of these moralities are different. Admissible on the higher morality is not what we mean around good.

Zaman Ali
June 17, 2010 at 7:54 am Log in to Reply Its very great tutorial , its helpful and very good. thank you for putting together this wonderful lessons.

C++ Tutorial and Online Ebook


June 29, 2010 at 4:56 pm Log in to Reply [...] 1.7 Forward declarations [...]

tcp
June 30, 2010 at 6:07 am Log in to Reply okay so sometimes when i get to int add(int x, int y); { return x + y; } the little dropdown-menu-checkbox doesnt appear. why is this? the little alert box to the left of the code is green, so its not registering as a problem, but it refuses to run the program unless int add is a dropdown item. (the code is perfect, the only difference between the sites code and my own is the dropdown box). the error message: error C2447: { : missing function header (old-style formal list?)

Auge
August 22, 2010 at 8:05 am Log in to Reply Lets have a look at your code: i n ta d d ( i n tx ,i n ty ) ; {
www.learncpp.com/cpp-tutorial/17-forward-declarations/ 20/24

2/16/13

1.7 Forward declarations Learn C++

r e t u r nx+y ; }

The semicolon after the function header must be dropped: i n ta d d ( i n tx ,i n ty )

The error message tells you that the compiler doesnt recognize to which function the part within the curly brackets belongs to. Thats also because of the semicolon after the function header.

StelStav
September 2, 2010 at 9:50 am Log in to Reply Hi all, I would like to ask what is the reasoning of c++ creators for forward declaration? Why isnt the compiler designed in such a manner so as to look for a function definition regardless of it being before or after the actual function calling? I would imagine that it is enough that the function definition resided in the same scope of the function call. Would the above (my) logic effect the efficiency of the executable or just the compilation? Thanks in advance very nice site btw, good job admins :)

Online Rollenspiele Kostenlos


September 7, 2010 at 11:00 pm Log in to Reply That was exactly what i needed ! Thank you for your support ! =)

Anders Melen
October 1, 2010 at 9:26 am Log in to Reply This tutorial is Perfect! Thank you so much for taking the time to help others learn a new great programming language! This could surly being published!, but thanks for making it available to all!

Steven
November 19, 2010 at 11:57 pm Log in to Reply Love the tutorials, It says to declare a function you dont need to define it. Then in the definition for function prototype its a declaration of a function that includes the functions name, parameters, and return type But when I try to compile

i n ta d d ( ) ;< b >i n tm a i n ( ) { . . .c o u t< <a d d ( 2 , 3 ) } < b >i n ta d d ( i n tx ,i n ty ) {r e t u r nx+y


www.learncpp.com/cpp-tutorial/17-forward-declarations/ 21/24

2/16/13

1.7 Forward declarations Learn C++

It tells me add() cant take 2 arguments, and forces me to declare i n ta d d ( i n tx ,i n ty )

at the beginning before it will compile/work. Wouldnt that be a declaration WITH a function prototype? or should a d d ( ) ;

work at the beginning and Im doing something wrong.

donblas
July 16, 2011 at 10:31 pm Log in to Reply i know its been a while since this was used, but how do you make a program that does more than add a bunch of numbers?

zingmars
July 19, 2011 at 2:48 am Log in to Reply Continue reading the tutorials, and youll understand how :)

sherwood
July 29, 2011 at 10:58 am Log in to Reply I did not understand the difference between Forward Declaration and Function Prototyping, there is a blog that explains this really well. http://blog.onaclovtech.com/2011/07/forwarddeclaration-vs-function.html basically Forward declaration has the function above main and function prototyping just has the single line prototype above main and the real function(definition) can be elsewhere.

misserwell
August 3, 2011 at 9:29 pm Log in to Reply Thank you very much, the article is so clear , hope more and more visitor knows the web

Da-Rage44
August 23, 2011 at 7:58 pm Log in to Reply I have a stupid question, what does it actually mean by the program never defines the function? Do we assume we need a return value to define a function?

www.learncpp.com/cpp-tutorial/17-forward-declarations/

22/24

2/16/13

1.7 Forward declarations Learn C++

zingmars
October 8, 2011 at 4:14 am Log in to Reply If you only make a forward declaration and never actually make the function the program never actually makes the function, meaning you will get errors when youre trying to use the forward declaration. No you dont need a return value to declare a function (void), it just needs to be declared somewhere (it doesnt even need to have code in it).

fould12
January 12, 2012 at 2:52 pm Log in to Reply add.obj : error LNK2001: unresolved external symbol int __cdecl add(int,int) (? add@@YAHHH@Z) (?add@@YAHHH@Z) Makes me think of add?YAHHH!!

equasar
February 17, 2012 at 9:06 am Log in to Reply On the Quiz question 2, shouldnt be forward declaration instead function prototype?

Daniel Russell Forward declarations


April 6, 2012 at 11:12 pm Log in to Reply [...] Quiz Answers 1) Show Solution [...]

Amjadb
April 24, 2012 at 8:50 am Log in to Reply Can i just make a function call like : int add(int x, int y); before the line of add (3, 4) ?

CSC209Cramming SLog of the most illogical mind


October 20, 2012 at 3:26 pm Log in to Reply [...] You can find nice example and quiz about it at here, http://www.learncpp.com/cpp-tutorial/17-forwarddeclarations/ [...]

You must be logged in to post a comment.

1.6 Whitespace and basic formatting

1.8 Programs with multiple files

www.learncpp.com/cpp-tutorial/17-forward-declarations/

23/24

2/16/13

1.7 Forward declarations Learn C++

www.learncpp.com/cpp-tutorial/17-forward-declarations/

24/24

You might also like