Error C2661 'div': no overloaded function takes 1 arguments [duplicate] - c++

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(){}

Related

Why does my C++ function, only when it's placed after main(), not work?

I recently picked up C++ and decided to try out making a function. However, I've run into a problem with my function func() where, even if declared beforehand, it only works if it's placed before the main() function.
If I place it after the main() function, the system tells me there is "no matching function for call to func".
Note: the functionfunc2 on the other hand works even if placed before or after the main() function.
So here's the code :
#include <stdio.h>
#include <iostream>
void func2();
int func();
int main()
{
int y=2;
std :: cout << "Hello World\n" << func(y) << "\n";
func2();
return 0;
}
int func(int x)
{
x *= 2;
return x;
}
void func2()
{
std :: cout << "Hello there";
}
In C language, the declaration int func(); means a function with an unspecified number of arguments of any type, returning a int.
In C++ language, the same declaration int func(); means a function without any arguments, returning a int.
And therefore, in C++, the definition of func with an argument of type int is an overload. For the compiler, it is a different function, which in the original code is not declared before use, so an error is emitted.
But in C, it would be perfectly legal.
int func();
and
int func(int x)
See the difference? The first one should be
int func(int x);
You told the compiler that func was a function with no arguments, then when you tried to call it with one argument the compiler said 'no matching function'.

why can we access a non_member function from member function in c++

The following code compiles without any warning or error:
#include <iostream>
using namespace std;
class demo_class
{
int x;
float y;
public:
void fun(void);
};
void fun2(void)
{
cout<<"i am fun2\n";
}
void demo_class::fun(void)
{
cout<<"i am fun\n";
cout<<"i can call fun2\n";
fun2();
}
int main()
{
demo_class ob1;
ob1.fun();
return 0;
}
I am not understanding that as the scope of fun function is only in demo_class
then how can it call fun2 function, should not it show error as the access of fun function only within the demo_class?
Name lookup would try to examine all the possible scopes, until it finds at least one at any scope, then the name lookup stops.
In this case, the name fun2 can't be found at class scope, then the further scope, i.e. globle scope is examined and ::fun2 is found.
There is no reason to disallow calling a free function from within a member function. If this was the case classes would be rather useless (they would be a way to prevent code reuse instead of supporting it).
As mentioned in a comment, in cout<<"i am fun2\n"; you are calling a non-member function and calling fun is not much different from that.
Further, with a grain of salt your example is not much different from
#include <iostream>
using namespace std;
class demo_class
{
};
void fun2(void)
{
cout<<"i am fun2\n";
}
void fun3(demo_class& dc)
{
cout<<"i am fun\n";
cout<<"i can call fun2\n";
fun2();
}
int main()
{
demo_class ob1;
fun3(ob1);
return 0;
}
A member function can always be transformed into a free function. If fun would access private members we would have to declare it as friend to make the above work, but otherwise no problem here.
You can also do the reverse and call member functions in free functions as in
struct foo {
void bar(){}
};
void func(foo& f) {
f.bar();
}
int main() {
foo f;
func(f);
}

C++ cant figure out whats wrong with my function call [duplicate]

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(){}

C++ error: 'count_of_function_calls' was not declared in this scope

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.

Visual Studio 2013 c++ function syntax error

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