"does not have a class type"错误
"does not have a class type" error
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
对象之后会被销毁。
相关文章:
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 将系数存储在头文件的数组中("does not name a type"错误)
- 尝试打开 ifstream 时出现"Incomplete type"错误
- 在 C++ 中使用命名空间时出现"does not name a type"错误
- 已声明时"Does not name a type"错误
- unordered_set .find 方法无法编译的 C++ "expression must have class type"错误
- 模板类和'invalid use of incomplete type'错误
- 在接口文件中使用模板时出现"not a type"错误的函数指针
- 我需要在不使用前向声明的情况下相互包含两个头文件,导致"incomplete type"错误
- 在 c++ 中,如果包含标头,为什么我会收到'does not a name of type'错误?
- 什么原因导致我的"‘FatalError’ in namespace ‘Foam’ does not name a type"错误?
- 为什么我会收到"Expression must have a class type"错误?(C++)
- 无法使用其他文件中的枚举:" does not name a type"错误
- 如何修复"static assertion failed: template argument not an integral type"错误?
- "Expression must have class type"错误
- "unresolved overloaded function type"错误并将运算符作为函数参数传递
- C++模板"class type"错误
- "Allocating an object of abstract class type"错误,尽管所有函数都有实现
- "does not have a class type"错误
- "expected unqualified-id before 'void'" & "string in class does not name a type" 错误