I'm using VS 2013, for some reason in a C++ console application a simple function declaration will not work. What is going on?
I have #include iostream and the code is inside the int main () {...} body.
-TSR
UPDATE:
Here is the full program
Look at the comment in my program. You should write functions outside of the main method.
/* Wrong code
-----------------------------------------
*/
#include <iostream>
int main()
{
int printmessage ()
{
}
}
/* Correct code
----------------------------------------
*/
#include <iostream>
int printmessage ()
{
}
int main()
{
}
You are not allowed to define a function within another function, although you are allowed to declare one. So something like this is allowed:
int main()
{
void func1() ; // function declaration but not definition.
}
void func1()
{
//...
}
but this is probably what makes more sense for you:
int printmessage()
{
//...
}
int main()
{
}
Function definitions are only allowed in the namespace or class scope, from the draft C++ standard section 8.4 Function definitions paragraph 2 says:
[...]A function shall be defined only in namespace or class scope.
You cannot have a function definition inside the main loop. You can either use function prototyping before the main(int argc, char** argv) or define the function before the main.
// Either define your function here
void Foo() { }
// Or use this prototyping
void Bar();
int main ()
{
// Call your function here
Foo();
return EXIT_SUCCESS;
}
void Bar() { }
Hope this helps.
For the declaration of function inside another function, it is just the backward compatibility with C programming. I doubt if anyone ever use this anymore. Plus, this is C++ not C
Related
Why can't I put a function after main, visual studio cannot build the program. Is this a C++ quirk or a Visual Studio quirk?
eg.
int main()
{
myFunction()
}
myFunction(){}
will produce an error that main cannot use myFunction
You can, but you have to declare it beforehand:
void myFunction(); // declaration
int main()
{
myFunction();
}
void myFunction(){} // definition
Note that a function needs a return type. If the function does not return anything, that type must be void.
You cannot use a name/symbol which is not yet declared. That is the whole reason.
It is like this:
i = 10; //i not yet declared
int i;
That is wrong too, exactly for the same reason. The compiler doesn't know what i is – it doesn't really care what it will be.
Just like you write this (which also makes sense to you as well as the compiler):
int i; //declaration (and definition too!)
i = 10; //use
you've to write this:
void myFunction(); //declaration!
int main()
{
myFunction() //use
}
void myFunction(){} //definition
Hope that helps.
most of the computer programming languages have top to down approach which means code is compiled from the top. When we define a function after main function and use it in the main [myFunction () ], compiler thinks " what is this. I never saw this before " and it generates an error saying myFunction not declared. If you want to use in this way you should give a prototype for that function before you define main function. But some compilers accept without a prototype.
#include<stdio.h>
void myFunction(); //prototype
int main()
{
myFunction(); //use
}
myFunction(){ //definition
.......;
}
Because
myFunction()
has to be declared before using it. This is c++ behaviour in general.
Functions need to be declared before they can be used:
void myFunction();
int main() {
myFunction();
}
void myFunction() {
...
}
you have to forward declare a function so main can know that there is some.
void myFunction();
int main()
{
myFunction();
}
void myFunction(){}
Don't forget about putting ; after each command.
specify the function declaration before calling function .So that compiler will know about return type and signature
Declare then define.
void func();
int main()
{
func();
}
void func()
{
}
You have to declare the function before.
void myFunction();
int main()
{
myFunction()
}
myFunction(){}
I have a file named test.cc
#include <stdio.h>
int doit(){
return 4;
}
namespace abc {
int returnIt(int a){
return a;
}
}
I can use doit(), but how can I use this function in namespace in my main.cc without using .h file:
using namespace abc;
int doit();
int main(int argc, const char * argv[]) {
cout<<returnIt(3)<<endl; // print 3
cout<<doit(); // print 4
return 0;
}
You can call functions by first declaring them. Example:
namespace abc {
int returnIt(int a); // function declaration
}
int main() {
abc::returnIt(3); // the declared function can now be called
Note that the declaration must be exactly the same as used elsewhere in the program. To achieve identical declarations across translation units, it is conventional to put the declaration into a separate file (called a header) and include that file using the pre-processor whenever the declaration is needed.
All you need is to simply write the functions before the main function. That way, the compiler has processed the function prototypes by the time it encounters them in main and can validate the function call.
int doit()
{
return 4;
}
int returnIt(int a)
{
return a;
}
int main(int argc, const char * argv[])
{
cout<<returnIt(3)<<endl; // print 3
cout<<doit(); // print 4
return 0;
}
In general, avoid using namespace;. It makes for code that can be broken or be rendered less readable due to incorrect variable/function usage. That is because too many symbols can occupy the same (global) scope.
If another library needs to used, as user4581301 pointed out, then it may be simpler to use eerorika answer/method.
I have a question regarding inline function.
#include <iostream>
using namespace std;
class A
{
public:
inline void fun()
{
int i=5;
cout<<i<<endl;
}
};
int main()
{
A a1;
int i = 2;
a1.fun();
cout<<i<<endl;
return 0;
}
In above program, when function fun() was called, the compiler should have made copy of this function and insert into main function body because it is inline.
So, I have a question, Why doesn't compiler give an error of variable int i; being re-declared?
You seem confused about scopes. They're not "within the same" scope
You can declare multiple variables with the same name at different scopes. A very simple example would be the following:
int main()
{
int a; // 'a' refers to the int until it is shadowed or its block ends
{
float a; // 'a' refers to the float until the end of this block
} // 'a' now refers to the int again
}
Inline expansion is not pure text replacement (as opposed to macros).
Whether a function is inlined or not has no effect on semantics Otherwise the program would behave differently depending on whether the function was inlined or not, and return statements would not behave properly at all.
Why can't I put a function after main, visual studio cannot build the program. Is this a C++ quirk or a Visual Studio quirk?
eg.
int main()
{
myFunction()
}
myFunction(){}
will produce an error that main cannot use myFunction
You can, but you have to declare it beforehand:
void myFunction(); // declaration
int main()
{
myFunction();
}
void myFunction(){} // definition
Note that a function needs a return type. If the function does not return anything, that type must be void.
You cannot use a name/symbol which is not yet declared. That is the whole reason.
It is like this:
i = 10; //i not yet declared
int i;
That is wrong too, exactly for the same reason. The compiler doesn't know what i is – it doesn't really care what it will be.
Just like you write this (which also makes sense to you as well as the compiler):
int i; //declaration (and definition too!)
i = 10; //use
you've to write this:
void myFunction(); //declaration!
int main()
{
myFunction() //use
}
void myFunction(){} //definition
Hope that helps.
most of the computer programming languages have top to down approach which means code is compiled from the top. When we define a function after main function and use it in the main [myFunction () ], compiler thinks " what is this. I never saw this before " and it generates an error saying myFunction not declared. If you want to use in this way you should give a prototype for that function before you define main function. But some compilers accept without a prototype.
#include<stdio.h>
void myFunction(); //prototype
int main()
{
myFunction(); //use
}
myFunction(){ //definition
.......;
}
Because
myFunction()
has to be declared before using it. This is c++ behaviour in general.
Functions need to be declared before they can be used:
void myFunction();
int main() {
myFunction();
}
void myFunction() {
...
}
you have to forward declare a function so main can know that there is some.
void myFunction();
int main()
{
myFunction();
}
void myFunction(){}
Don't forget about putting ; after each command.
specify the function declaration before calling function .So that compiler will know about return type and signature
Declare then define.
void func();
int main()
{
func();
}
void func()
{
}
You have to declare the function before.
void myFunction();
int main()
{
myFunction()
}
myFunction(){}
hi I am new to c++ and I am stuck in a question. I am a beginner, please help me, that you.
#include <iostream>
using namespace std;
int dostuff ()
{
return 2 + 3;
}
void fun ()
{
count_of_function_calls++;
}
int main()
{
void fun ();
void fun ();
void fun();
cout << "Function fun was called" << count_of_function_calls << "times";
}
Many, many problems, you should definitely read a C++ book or reread some tutorials
Where did you define count_of_function_calls?
Nowhere, that's why the compiler is complaining. You always have to declare variables before you use them:
int count_of_function_calls = 0;
Note that in your case, because you want to value of count_of_function_calls to be incremented for each function call, you should declare it as a global variable (this is not recommended, consider using something else).
A global variable is declared outside of any scope, in your case, you could for example defined it just above void fun ().
void fun (); declares a function (called fun), taking no arguments and returning void. It doesn't call the function fun. If you want to call a function, you don't have to specify the return type:
//Call function 'fun'
fun();
I think you forgot to define global variable count_of_function_calls
For example
#include <iostream>
using namespace std;
int count_of_function_calls;
int dostuff ()
{
return 2 + 3;
}
void fun ()
{
count_of_function_calls++;
}
//...
And the function calls must look like
fun();
This
void fun ();
is a function declaration. It is not a call of the function.