-1

Im new to C++ and started working with classes recently for a school excercice. I really cant see whats wrong and after creating an object "player" to the Hero class i can't use that object later in the "main Menu" function to call a method because i get the "identifier is undefined" error!

Any suggestions?

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class Hero
{   
    private:
        //member variables
        string playername;

    public:
        //constructor
        Hero(string name)
        {
            playername = name;
        }

        string getName()
        {
            return playername;
        }

};

//start 1
void mainMenu()
{
    cout << " -  -  - |" << player.getName() << "-  -  - \n";
}

void setPlayer()
{
    string name;
    cout << "Hello, what is your name? " << endl;
    getline(cin, name);

    Hero player(name);
    mainMenu();
}

int main()
{
    int selection;

    cout << "Shadow of darkness\n ";

    cout << "1.) Start ";
    cout << "2.) Exit ";

    cin >> selection;

    if (selection == 1)
        setPlayer();

    else if (selection == 2)
        exit (0);

    else
        main();

    return 0;
}
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
Daniel B
  • 111
  • 1
  • 2
  • 8
  • 2
    Get a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and don't skip chapters. And press the return key fewer times and more systematically. – LogicStuff May 10 '17 at 19:04
  • Your question falls into a category that makes [this link](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) a must read. – StoryTeller - Unslander Monica May 10 '17 at 19:04
  • 1
    `player` is declared within the scope of `setPlayer`, identifiers are bound to the scope in which they're declared. It seems like you're missing a lot of info looking at the other code, learning by doing _and_ guessing doesn't really work in C++. – George May 10 '17 at 19:06
  • okay thanks im used to python and have actually created a game with pygame but this is just so different, in a bad way:) – Daniel B May 10 '17 at 19:22

1 Answers1

0

OK, calling main() from main() is a forbidden (as explained here), so do not do it.

Here is a typical example with your class (the class is cool as you have it, I just added an initializer list for fun):

#include <iostream>
#include <string>
using namespace std;

class Hero
{
    private:
        //member variables
        string playername;
    public:
        //constructor
        Hero(string name) : playername(name)
        {
        }

        string getName()
        {
            return playername;
        }
};

int main()
{
    Hero player("Daniel");
    cout << "Player's name: " << player.getName() << std::endl;
    return 0;
}

Output:

Player's name: Daniel

Based on this, try to work your logic and do all sort of stuff that you long for (after reading some books/tutorials)!

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305