C++ C++;错误C2227:x27的左侧-&燃气轮机;健康';必须指向类/结构/联合/泛型类型

C++ C++;错误C2227:x27的左侧-&燃气轮机;健康';必须指向类/结构/联合/泛型类型,c++,pointers,compiler-errors,dependencies,forward-declaration,C++,Pointers,Compiler Errors,Dependencies,Forward Declaration,所以这里的问题是玩家需要一张卡,因此,卡需要在玩家类的顶部声明。进一步讲,我的Card类使用了一个需要播放器指针参数的函数。为了消除其他错误,我在Card类上方使用了一个转发声明,以使Player类可见。我还在Attacknemy函数参数中使用一个指向播放器的指针,因为在该点上,仅通过向前声明无法知道对象的大小。当我试图从卡中Attacknemy函数中传递的播放器指针调用函数时,我得到一个编译错误。错误是错误C2227:“->looseHealth”的左侧必须指向class/struct/uni

所以这里的问题是玩家需要一张卡,因此,卡需要在玩家类的顶部声明。进一步讲,我的Card类使用了一个需要播放器指针参数的函数。为了消除其他错误,我在Card类上方使用了一个转发声明,以使Player类可见。我还在Attacknemy函数参数中使用一个指向播放器的指针,因为在该点上,仅通过向前声明无法知道对象的大小。当我试图从卡中Attacknemy函数中传递的播放器指针调用函数时,我得到一个编译错误。错误是错误C2227:“->looseHealth”的左侧必须指向class/struct/union/generic type

以下是节目:

#include "stdafx.h"
#include <iostream>
using namespace std;
class Player;
class Card {
private:
    int attack;
public:
    Card() {
        this->attack = 2;
    }

    void attackEnemy(Player* i) {
        i->looseHealth(this->attack); //error is here
    }
};

class Player {
private:
    string name;
    int health;
    Card* playersCard;
public:
    Player(string name) {
        playersCard = new Card();
        this->name = name;
    }

    void looseHealth(int x) {
        cout << "lost health -" << x << " points" << endl;
        health -= x;
    }
};

int main()
{
    Card* opponedsCard = new Card();
    Player* player1 = new Player("player 1");
    opponedsCard->attackEnemy(player1);
    return 0;
}
#包括“stdafx.h”
#包括
使用名称空间std;
班级运动员;
班级卡{
私人:
智力攻击;
公众:
卡片(){
这个->攻击=2;
}
无效攻击敌人(玩家*i){
i->looseHealth(此->攻击);//错误在这里
}
};
职业选手{
私人:
字符串名;
国际卫生组织;
卡片*玩家卡片;
公众:
播放器(字符串名称){
玩家卡=新卡();
此->名称=名称;
}
无效健康(int x){

coutattackEnemy
使用了一个不完整的类型
Player
,它是向前声明的

只需在
Card
类中声明
void attackEnemy(玩家*i);
移动

在定义
玩家
类之后

void Card::attackEnemy(Player* i) {
        i->looseHealth(this->attack); //error is here
    }

编译器在您使用它时不知道什么是
looseHealth
。该函数稍后在源模块中定义。是时候学习如何创建单独的
.cpp
.h
文件,并将函数体移出类定义。在出现错误的行中,编译器需要已经看到
职业球员的完整定义
void Card::attackEnemy(Player* i) {
        i->looseHealth(this->attack); //error is here
    }