-2

I'm making a class that displays a message to the user and asks them if they want to return to the start of the program, but the message function is in a separate class from where my main() is located. How do I access the main() function from a different class?

This is an example of the stuff I want to do:
main.cpp file:

int main()
{
    Message DisplayMessage;
    DisplayMessage.dispMessage();
    return 0;
} 

Message.cpp file:

void dispMessage(void)
{
    cout << "This is my message" << endl;
    //now how do I call main again in the main.cpp file?
}

Thanks!

davidpox
  • 135
  • 1
  • 5
  • 18
  • 3
    I'm not sure if or how this works, but anyway I think you should not be doing this, as there is surely a more elegant solution. – Misch Dec 02 '14 at 13:03
  • You do not call main, *ever*. – Borgleader Dec 02 '14 at 13:05
  • You can use a boolen value, which your `dispMessage` returns. Maybe:`bool dispMessage(void){..//if you want to show this again return true; if not return false.}` and in your main: `while(dispMessage(){})` or something like that should give the same behavior than calling int main() again – Matthias Dec 02 '14 at 13:05
  • main as the [entry point](http://en.wikipedia.org/wiki/Entry_point) of an application should not be directly called. – wonko realtime Dec 02 '14 at 13:05
  • what do you really want to do? – wonko realtime Dec 02 '14 at 13:06

5 Answers5

4

you don't, change the return value of dispMessage to an int or similar, from the main you check the return code and do different actions based on that.

DRC
  • 4,898
  • 2
  • 21
  • 35
  • but how do I execute all of main again? say if I had a `cout` statement in my main, how can I do it so it prints that after the user has decided to run the program again from a separate class? – davidpox Dec 02 '14 at 13:57
4

main is special, you're not allowed to call it in C++.

So the "obvious" thing to do is to move everything to another function:

int my_main()
{
    Message DisplayMessage;
    DisplayMessage.dispMessage();
    return 0;
} 

int main() 
{
    return my_main();
}

Now you can call my_main from anywhere you like (as long as you declare it first in the translation unit you want to call it from, of course).

I'm not sure whether this will really solve your problem, but it's as close as possible to calling main again.

If you call my_main from somewhere else in your program then you won't exactly be "returning to the start", you'll be starting a new run through the code without having finished the old one. Normally to "return to the start" of something you want a loop. But it's up to you. Just don't come crying to us if you recurse so many times that you run out of stack space ;-)

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • This is great so far, thanks! But how do I call this my_main() from a separate cpp file? – davidpox Dec 02 '14 at 14:12
  • 1
    @davidpox: consult your C++ textbook or tutorial for how to organise functions in files and call them. A comment probably isn't enough to explain everything you need to know. The declaration you need is `int my_main();` – Steve Jessop Dec 02 '14 at 14:17
2

In C++ it is illegal for a program to call main itself, so the simple answer is you don't. You need to refactor your code, the simplest transformation is to write a loop in main, but other alternatives could include factoring the logic out of main into a different function that is declared in a header and that you can call.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • Why is it illegal to call main? – davidpox Dec 02 '14 at 13:23
  • 2
    @davidpox: I'm not certain, but I *think* the motivation is to free the implementation to do extra, hidden things in the `main` function that set up and take down the C++ runtime, while retaining `main` as the external entry point that the OS calls to start the program. Possibly implementations could do this and still ensure that `main` can also be called from inside the program without doing the extra stuff a second time, but the standard often comes down on the side of implementation convenience. Short answer is, "because the standard says so". – Steve Jessop Dec 02 '14 at 13:55
0

Maybe something like that:

bool dispMessage(void)
{
  cout << "This is my message" << endl;
  // call me again
  return true;

  // do not call me again
  return false;
}

and in the int main():

int main()
{
  Message DisplayMessage;
  while(DisplayMessage.dispMessage())
  {

  }
  return 0;
} 
Matthias
  • 908
  • 1
  • 7
  • 22
0

Assuming that you could change dispMessage(void) to something like bool askForReturnToStart() then you could use that to build a loop within main, f.e.:

int main() {
  Message dm;
  do {
    // whatever
  } while (dm.askForReturnToStart());
}
wonko realtime
  • 545
  • 9
  • 26