简体   繁体   中英

Error request for member in which is of non class type - problem with using method on copied object

I'm just learning C++. I want to create a fighting-text game. In the beginning, I created fighters. Then I ask the player to choose a fighter and I create a new object which stores information about the selected fighter. This is my code:

fighter.h

#include <string>
using namespace std;
class Fighter
{
        int charNumber;
        string name;
        int attack;
        int def;
        int hp;
        int power;
        int regeneration;
        string background;
        int choosingWarrior;
    public:

        Fighter(int charNumber = 99, string name = "Default" , int attack = 10, int def = 10, int hp = 10, int power = 10, int regeneration = 10, string background = "default");
        ~Fighter();

        int showInfo() const;
        void chooseCharacter();
};

fighter.cpp

#include "fighter.h"
#include <iostream>
#include <string>
Fighter::Fighter(int charNumber, string name, int attack, int def, int hp, int power, int regeneration, string background) {
    this->charNumber = charNumber;
    this->name = name;
    this->attack = attack;
    this->def = def;
    this->hp = hp;
    this->power = power;
    this->regeneration = regeneration;
    this->background = background;
}
Fighter::~Fighter()
{

}
int Fighter::showInfo() const
{
    cout << "Fighter number: " << charNumber << endl;
    cout << "Fighter name: " << name << endl;
    cout << "Statistics:" << endl;
    cout << "Attack: " << attack << endl;
    cout << "Defense: " << def << endl;
    cout << "Health: " << hp << endl;
    cout << "Power: " << power << endl;
    cout << "Regeneration: " << regeneration << endl;
    cout << "Background: " << background << endl;


}

main.cpp (main.cpp|39| error: request for member 'showInfo' in 'choosingFighter', which is of non-class type 'Fighter (int, std::__cxx11::string, int, int, int, int, int, std::__cxx11::string)' {aka 'Fighter(int, std::__cxx11::basic_string, int, int, int, int, int, std::__cxx11::basic_string)'}| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

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


/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
    Fighter Scorpion(1, "Scorpion", 100, 100, 100, 100, 100, "Hanzo Hasashi, better known as Scorpion, is a resurrected ninja in the"
                  "Mortal Kombat fighting game series as well as the mascot of the games."
                    "He is one of the very few original characters debuting in the first"
                    "Mortal Kombat arcade game. He holds the distinction, along with"
                    "Raiden and Sub-Zero (in one form or another),"
                    "of appearing in every generation of Mortal Kombat games as a playable character." );
    Fighter Shodan(2, "Shodan", 90, 110, 110, 90, 100, "S.H.O.D.A.N. (Sentient Hyper-Optimized Data Access Network),"
                   "later referred to as SHODAN"
                   "is an Artificial Intelligence and the main antagonist of the System Shock series."
                   "She is voiced by game writer and designer Terri Brosius." );
    Fighter Legion(3, "Legion", 110, 90, 90, 110, 100, "Legion is the name taken by the gestalt consciousness formed by 1,183 geth"
                   "programs inhabiting a unique geth \"mobile platform\"." );
    Fighter choosingFighter(int charNumber, string name, int attack, int def, int hp, int power, int regeneration, string background);
    Scorpion.showInfo();
    Shodan.showInfo();
    Legion.showInfo();
    int choice;
    cout << "Choose your character (write a number) 1. Scorpion; 2. Shodan; 3. Legion: " << endl;
    cin >> choice;
    do
    {
    if (choice == 1)
        Fighter choosingFighter = Scorpion;
    else if (choice == 2)
        Fighter choosingFighter = Shodan;
    else if (choice == 3)
        Fighter choosingFighter = Legion;
    else
        cout << "Wrong number try again";
    } while (choice != (1 || 2 || 3));
    choosingFighter.showInfo();


    return 0;
}

What is wrong? In this part of the code, I created a new object

Fighter choosingFighter(int charNumber, string name, int attack, int def, int hp, int power, int regeneration, string background);

Here I copy my new object based on the fighter that the player chose

   if (choice == 1)
        Fighter choosingFighter = Scorpion;
    else if (choice == 2)
        Fighter choosingFighter = Shodan;
    else if (choice == 3)
        Fighter choosingFighter = Legion;
    else
        cout << "Wrong number try again";
    } while (choice != (1 || 2 || 3));

And here I use my method, but I get an error:

choosingFighter.showInfo();

(Error request for member in which is of non class type)

What I should change in my code?

First you declare choosingFighter as a function . Then inside the if statements you define a set of local variables (inside the if statememt. and whose life-time ends with the if statement).

For example:

if (choice == 1)
    Fighter choosingFighter = Scorpion;

is the same as

if (choice == 1)
{
    Fighter choosingFighter = Scorpion;
}

Adding the braces should make it more clear that you define a brand new variable inside a brand new scope. And that should also make it clearer that the variable will cease to exist once the block ends with the closing } .


Now for the solution.

First of all I recommend you define choosingFighter as a pointer , so we can make it point to the objects instead of making copies:

Fighter* choosingFighter = nullptr;

Then inside the if statement make it point to the selected object:

if (choice == 1)
{
    choosingFighter = &Scorpion;
}

And finally check that the pointer is valid before using it:

if (choosingFighter)
{
    choosingFighter->showInfo();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM