0

I want to call the function show() by calling the function folder() with the help of an object. obj.folder() -> show(); Here, the function show() is not doing its job.

#include<iostream>
using namespace std;
int count=0;

class file{
    int roll;
    public:
        void folder()
        {
            ++count;
            roll=count;
            void show();
        }
        void show(){cout<<roll<<endl;}
};
int main()
{
    file obj;
    obj.folder();
}
mch
  • 9,424
  • 2
  • 28
  • 42
  • Get rid of the `void` keyword when calling `show()` inside `folder()` – RKest Apr 19 '23 at 12:32
  • Also remove `using namespace std;` as `std::count` also exists and causes warnings - live - https://godbolt.org/z/ac11WMK5o – Richard Critten Apr 19 '23 at 12:34
  • 1
    Calling syntax a function is explained in any [beginner c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Apr 19 '23 at 12:34
  • 1
    `void show();` is a function declaration. `show();` is a function call. – molbdnilo Apr 19 '23 at 12:39

2 Answers2

4

The problem is that your function calling syntax (void show();) is incorrect. By writing void show(); you're actually declaring a global free function named show that has no parameters and has a return type of void.

The correct way to call the function would be to remove the void, as shown below:

void folder()
{
//-v---------->removed void from here   
   show();
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jason
  • 36,170
  • 5
  • 26
  • 60
1

My dude, you did not call show inside folder, u just declared a global function void show() instead.