Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, September 2, 2016

C++ Identifier

C++ Identifier "_var" is undefined


I am attempting to learn C++ (currently only know PHP and some C#) and have run into my first issue.

I am trying to call a class inside a switch, then use that defined variable after the switch. However, I get the error described in the title.

#include   #include     using namespace std;    class Hero {  protected:      int hHealth,hStamina,hExp;      string hName;  public:      void Create(string);      string GetName() {          return this->hName;      }  };    class Wizard:public Hero {  public:      void SetStats(string hName) {          this->hName = hName;            this->hHealth = 40;          this->hStamina = 80;      }    };    int main() {      string hName;      int hClass;          cout << "Welcome to Ryan's Dungeons & Dragons Adventure!\n\n";      cout << "Enter your Heroes name\n";      cout << "Name: ";      cin >> hName;        cout << hName << ", please select your class\n";      cout << "(1) The Wizard\n";      cout << "(2) The Warrior\n";      cout << "(3) The Rogue\n";      cout << "(4) The Priest\n";        cout << "Class: ";      cin >> hClass;        switch (hClass) {      case 1:          Wizard _hero;          break;      }        cout << _hero->GetName();          system("PAUSE");      return 0;  }  

The error in question occurs on the line:

cout << _hero->getName();

where it says _hero is undefind.

Answer by Mahesh for C++ Identifier "_var" is undefined


switch (hClass) {      case 1:          Wizard _hero;          break;  } // <-- _hero is deallocated at this point    cout << _hero->GetName();  

The scope of _hero is limited to the switch statement.

Answer by yan for C++ Identifier "_var" is undefined


_hero is defined only within the scope of that switch statement. You need to create objects in the same or higher up scope that you'll be using them.

One way you can get around this is define a pointer to Hero before the switch (initializing to null) and then set it to a value inside the switch. For instance:

Wizard *_hero = NULL;  switch (hClass) {      case 1:          _hero = new Wizard();          break;      }  }    if (_hero) {      cout << _hero->GetName();  }  

You're also using the -> on a class value (as opposed to a pointer to one). Scope issues aside, you probably intended to write _hero.GetName(). Inside your class, -> is right however since this is a pointer to your object.

Answer by Ryan for C++ Identifier "_var" is undefined


The _hero object is restricted to the scope of that switch block. What you want is probably this:

Hero* _hero;    switch (hClass) {  case 1:      _hero = new Wizard();      break;  }    cout << _hero->GetName();  

Answer by Xeo for C++ Identifier "_var" is undefined


I don't think that even works in C#... what you want is a pointer that's going to be initialized in the switch statement:

Hero* _hero = 0;    switch(hClass){    case 1: _hero = new Wizard;    break;  }    // use _hero ...    // at the end, delete it  delete _hero;  

Though, you now most likely need a virtual destructor and virtual functions. Read up on them, they're a powerful OO feature. But you probably know about them from C#.

Answer by Mayank for C++ Identifier "_var" is undefined


You said you know some C# and php, which I do not. I just want to know how would this have behaved in C#.

Creating an object inside some scope and using it outside the scope. Like: {int a;} a = 0;

In C++ its an issue.

    switch (hClass) {      case 1:          Wizard _hero;          break;      }  //At this no _hero is present. _hero is out of its scope  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.