In C when you use a function before declaring it the compiler assumes that is takes no parameters and returns and int.
If your function returns another type or takes parameters then the compiler produces an error.
Does the same happen in C++ if I make an object of a class that is declared later on in the code?
-
3BTW, implicit declaration has become invalid (not supported by standard) since `C99`. Just saying. – Sourav Ghosh Jul 27 '15 at 11:31
-
Nope C++ will be fine and not throw an error, however if you declare a function in a header file link the header file to a cpp file and not declare the function C++ will throw an error which sometimes can be very misleading – Canvas Jul 27 '15 at 11:33
-
@Canvas The class definition must be available before an instance of that class is made. You can't "make an object of a class that is declared later on in the code". (unless it has already been defined, in which case the declaration is unnecessary.) – juanchopanza Jul 27 '15 at 11:34
-
@juanchopanza Sir, I'm not a C++ person, so if you kindly add a bit about C.1.4, `C++11`, it will be helpful for me to understand. – Sourav Ghosh Jul 27 '15 at 11:38
-
@SouravGhosh The same applies in C. You cannot instantiate a type without having the definition of that type. – juanchopanza Jul 27 '15 at 12:11
-
@juanchopanza No, I know about C, i was asking about C++, explicitly. – Sourav Ghosh Jul 27 '15 at 12:18
-
Does this answer your question? [Does class/function order matter in C++?](https://stackoverflow.com/questions/26122024/does-class-function-order-matter-in-c) – Zoe Oct 24 '20 at 08:42
3 Answers
In C++ there is no implicit declaration, neither of classes nor of functions. So your question does not apply directly.
If you call a function or create an object of a given type, that function/type has to be declared before the first use. Functions do not necessarily have to be defined. and classes only have to be defined when actually instantiating them, or using them as (member) variables. When only a pointer to a class is needed a declaration is not needed, as a forward-declaration is sufficient until the memory of the actual object is allocated or functions are called on the object.

- 5,744
- 30
- 49
-
If you want to create an object of a given type, the type *has* to be defined. – juanchopanza Jul 27 '15 at 12:07
-
Yes, but I think is should be "full definition" instead of "full declaration", although just "definition" would do fine. – juanchopanza Jul 27 '15 at 12:30
The order does matter. If you reference a function which has not been declared (just the signature and return type, no implementation) then the compiler will throw an error. The definition of your function can wait until link time. AFAIK, there is not implicit declaration in C++.
Usually you put the declarations of your functions in header files. Traditionally, the symbols exported by a translation unit (usually a stand-alone source file e.g., hello.cpp
) will be made available through a similarly-named header file (e.g., hello.h
). The implementation then follows in the source file. Every translation unit can then include header files from other translation units (e.g. other.h
).
Every translation unit gets compiled individually (i.e. a source file such as hello.cpp
; all #include
preprocessing statements are replaced by the actual contents of the files to be included). At link time, the implementations of the functions in different translation units get linked together. If this linking step fails, then you can still encounter errors.

- 301
- 2
- 7
In early c++ version, if you define the function test(int a)
, it will return a int
type value by default. But after c++ becoming standard, the function will get an error. You can find the introduction in the book c++ Primer
, the function chapter
.

- 502
- 1
- 5
- 20