-4

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"; 
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

2 Answers2

2

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();
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335