C++ 错误 C2227:“->looseHealth”左侧必须指向 class/struct/union/generic 类型

C++ error C2227: left of '->looseHealth' must point to class/struct/union/generic type

所以这里的问题是一个Player需要一张Card,因此需要在Player之上声明Card class。在我的 Card class 上进一步使用了一个需要 Player 指针参数的函数。为了消除其他错误,我在 Card class 上方使用了前向声明以使 Player class 可见。我还在 attackEnemy 函数参数中使用了一个指向 Player 的指针,因为此时仅通过前向声明无法知道对象的大小。当我尝试从 Card 内的 attackEnemy 函数中传递的 Player 指针调用函数时,出现编译错误。错误是 error C2227: '->looseHealth' 的左侧必须指向 class/struct/union/generic 类型 .

这是程序:

#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;
}

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

只需在 Card class 中声明 void attackEnemy(Player* i); 和 移动

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

定义后Playerclass

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