"does not have a class type"错误

"does not have a class type" error

本文关键字:type 错误 class have does not      更新时间:2023-10-16

player class> -

#ifndef PLAYER_H
#define PLAYER_H
class Player : public sf::Drawable, sf::Transformable
{
    public:
        Player(int indexWd, int indexHi);
        bool load();
};

game 类: -

#ifndef GAME_H
#define GAME_H
#include "Player.h"
#include <SFML/Graphics.hpp>
class Game
{
    public:
        Game();        
        Player player(int indWd, int indHi);
        void load();
};
#endif // GAME_H

内部游戏CPP文件: -

#include "Game.h"
Game::Game()
{
}
void Game::load()
{
    player(world.getIndexWd(), world.getIndexHi());
    player.load(); //gives error
}

在player.load()中,在上述方法中,编译器给出以下错误: -

错误:'((game*)this) -> game :: player'没有类型|

为什么会发生此错误?

您已经声明了一个名为 player的成员函数,并且您正在尝试将其使用,就像它是变量一样。

我猜它应该是数据成员:

Player player;

load功能中初始化:

player = Player(world.getIndexWd(), world.getIndexHi());
player.load();

尽管它没有默认的构造函数,但它不起作用,因此必须在构造函数中进行初始化:

Game::Game() : player(world.getIndexWd(), world.getIndexHi()) {}

此时仅在world进行初始化时才能起作用。

重写为:

void Game::load()
{
    Player p = this->player(world.getIndexWd(), world.getIndexHi());
    p.load();
}

player是当前类中的成员函数,您无法在其上应用.load

您没有定义您的功能

Player player(int indWd, int indHi);

在您的.cpp文件中。

还需要在Player类中使其静态,或使用对象调用它。

Player p = player(world.getIndexWd(), world.getIndexHi());
p.load();

也可能是

player(world.getIndexWd(), world.getIndexHi()).load();

,但我不建议使用它,因为您的Player对象之后会被销毁。