C++ functions problem - c++

Is there a way for functions to call each other i.e.
void menu()
{
some code here
play();
...
}
int play()
{
...
menu();
...
return 0;
}

Add the declaration of the second function at the top of your code file:
int play();
void menu()
{
// some code here
play();
// ...
}
int play()
{
// ...
menu();
// ...
return 0;
}
This is called a forward declaration, and it informs the compiler that an identifier will be declared later.
It is a way of denoting a function so that you can call it before you provide the complete definition.

Yes, but this is almost never what you want to do since careless use will break the stack.

Related

Error C2661 'div': no overloaded function takes 1 arguments [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++ 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(){}

CodeBloks C++ Error: Was not declared in the scope

Hey guys I'm trying to learn C++ and I was doing pretty well until I hit this wall..
I am getting two errors:
error: 'enter' was not declared in this scope
error: 'Satisfies' was not declared in this scope|
Here is my file. Why is that?
include <iostream>
using namespace std;
int main() {
while (1){
char menu;
cin>>menu;
switch (menu){
case 1: Enter(); break;
case 2: Satisfies(); break;
case 3: break;
};
};
}
int Enter(){
return 0;
}
int Satisfies(){
return 0;
}
You have to declare the functions before their usage for example before main and use the correct syntax of calling functions.
For example
//...
int Enter(){ return 0; }
int Satisfies(){ return 0; }
//...
int main()
{
//...
case 1:
Enter();
//...
You need to declare the functions before they are used. So put this above main:
int Enter();
int Satisfies();
You can leave the definitions (the bit that actually contains the code to run when the function is called) where they are. Or you can just move those functions above main, since a function definition is also a declaration.
The compiler needs to see these bits before it tries to call the function, so that it can know what arguments it needs, and what will be returned.
See this question.
You need to declare (or declare and definite) a function before you use it. And you need to make function calls with (). Like Enter() and Satisfied(). If you want to learn good programming and C coding and then go to C++ read "C for Dummies" by Dan Godkin. My favourite coding book.
You have 3 ways to do this and fix your code:
1. Write prototy definitions:
#include <iostram>
int Enter();
int Satisfies();
using namespace std;
int main()
{
//bla
}
int Enter(){ return 0; }
int Satisfies(){ return 0; }
2. Make a function.h file and put the declarations there. Save it in the same folder as the c / cpp file
then include in your code
#include "function.h"
3 Put your functions in order of execution in the c/cpp file. A function must be declared before it is used. Example:
void Enter()
{
//bla
}
void Satisfied()
{
//blub
}
int main()
{
Enter();
Satisfied();
}
More tricky example, when a function (Satisfied) uses an other function (Enter) the Enter function must be declared before the Satisfied function:
void Enter()
{
//bla
}
void Satisfied()
{
//blubb
Enter(); //now Enter must be declared before is Satisfied() is defined, so it must be "over" it in the source like in this example
}
int main()
{
Enter();
Satisfied();
}
Function call need to have () after the function name doesn't matter whether it take any parameter or not. Plus you need to define function header before main()
include <iostream>
using namespace std;
int Enter();
int Satisfies();
int main() {
while (1){
char menu;
cin>>menu;
switch (menu){
case 1: Enter();
break;
case 2: Satisfies();
break;
case 3:
break;
};
};
}
int Enter(){
return 0;
}
int Satisfies(){
return 0;
}

C++ help writing a class with declaration on header file

Well, hi there.
I'm new to c++ and I'm having some issues that I'm not sure what is causing them.
This is my main:
#include "GameWindow.h"
int main(void)
{
GameWindow * game_window = new GameWindow(true);
/* loop the game */
while (game_window->GetRunning())
{
// update
game_window->Update();
// draw
game_window->Draw();
}
delete game_window;
return 0;
}
and this is my header:
class GameWindow
{
private:
bool _running;
//GLFWwindow* _window;
public:
void SetRunning(bool new_val);
bool GetRunning();
GameWindow(bool running);
void Draw();
void Update();
}
and my c++ file:
#include "GameWindow.h"
void GameWindow::SetRunning(bool new_val)
{
_running = new_val;
}
bool GameWindow::GetRunning()
{
return _running;
}
GameWindow::GameWindow(bool running) :
_running(running)
{
}
void GameWindow::Draw()
{
}
void GameWindow::Update()
{
}
While going through all of this I find it tough to find why Visual Studio refuse to compile this code.
It's raising errors about how 'SetRunning' is overloading a function which differs only in return values, and that the return type of main should be Int and not GameWindow, and with all of this I just went completely lost.
Tried to put 'SetRunning' as a comment to simplify the issue but instead it raised the same on 'GetRunning' instead.
I'm guessing it's a really stupid mistake that is easy to fix, but still, can't find it.
Thank you for your time, and I'll appreciate any kind of help.
Missing ; at the end of class definition.
class GameWindow
{
// .....
}; // Missing semi-colon
Missing ; in class defination
{
};
because of this when you include the file in program then compiler not found the end of the class hence it says return type of main should be int not GameWindow

How do I make a function only callable from another function?

//function declerations
void func_A();
void func_B();
void func_SubA();
//main
int main(){ ... }
//function definitions
void func_A(){ ... }
void func_B(){ ... }
void func_SubA(){ ... }
What is the best way of ensuring that func_SubA() can only be called inside of func_A()?
I would like to receive a compiler error if func_B() attempted to call func_SubA().
You can declare the functions you want to restrict access to as private members of a class and then use friendship to grant access.
class Balloon
{
private: // private is the default. we just want to be explicit
friend void Ken();
friend void Barbie();
static void Pop();
};
void Ken() {
Balloon::Pop(); // We are allowed to pop it!
}
void Barbie() {
Balloon::Pop(); // We are allowed to pop it too!
}
void Jack() {
Balloon::Pop(); // Access Denied! we must be in a time out!
}
Another way is placing the definition of func_A() and func_SubA() in a different translation unit (cpp file) and making func_SubA() invisible outside this file by either making it static or placing it in an anonymous namespace:
// FILE main.cpp
//function declarations: possibly in a header file
void func_A();
void func_B();
// don't declare func_SubA() here
int main(){ /* ... */ }
void func_B(){ /* ... */ } // could also have its onw cpp file
// EOF main.cpp
// FILE func_A.cpp
static void func_SubA(){ /* ... */ }
/* or
namespace {
void func_SubA(){ ... }
}
*/
void func_A(){ /* calls func_SubA() */ }
// EOF func_A.cpp
Another way, as suggested by JesseTG and which I'm just making clear here, is using lambda expressions:
//function declarations
void func_A();
void func_B();
// don't declare func_SubA();
int main(){ /* ... */ }
//function definitions
void func_A(){
// define func_SubA through a lambda
auto func_SubA = [](){ /* the body of func_SubA */ };
// ...
func_SubA(); // calls func_SubA()
// ...
}
void func_B(){ /* ... */}
You can either do this the usual object-oriented way (private methods, etc.) or you can use lambda functions, which is a new feature in C++11. You can read up on them here or here, they're not difficult to use. Lambda functions allow you to access variables that are local to whatever block of code holds them.
Function declarations can be local, too. If you put the forward declaration for func_SubA inside funcA(), you should be good.