c++ 使用类对象作为类成员

鲍勃

“includes.h”是我将所有头文件和命名空间放入的头文件。

hit_box.h

#include "cludes.h"
class hit_box{
    friend class player;
private:
    /*class members*/
public:
    // without this I get "no default constructor" error. 
    hit_box(); 
    //I think having two constructors will create problems but that's a problem on top of my current problem 

    hit_box(char typ, int t, int x, int y, float Px, float Py,int pw, float spotx, float spoty);
}

播放器.h

    #pragma once
#include "cludes.h"

class player {
public:
    enum states { falling, hurt, landed, jumping };
    player(float a, float b, int f, sf::Texture t, sf::Texture t2, hit_box pnch);
private:
    //other class members
    hit_box punch;
}

播放器.cpp

#include "player.h"
#include "cludes.h"
player::player(float a, float b, int f, sf::Texture t, sf::Texture t2, hit_box pnch){
    /*other class members*/
    hit_box punch = pnch;
}

我想hit_box成为我可以在播放器类函数中调用的成员。这些是我收到的错误消息

Error C2061    syntax error: identifier 'hit_box' 
Error C3646    'punch': unknown override specifier
Error C4430    missing type specifier - int assumed. Note: C++ does not support default-int

我试图hit_box通过

播放器.h

#include "cludes.h"
class player {
    hit_box punch;
public:
player(float a, float b, int f, sf::Texture t, sf::Texture t2);
}

播放器.cpp

#include "player.h"
#include "cludes.h"
player::player(float a, float b, int f, sf::Texture t, sf::Texture t2) : punch('!', 10, 30, 64, -10, -10, 20, 4.44, 4.44){
//other class members
}

hit_box这种方式没有默认构造函数,只有非默认构造函数。不管我还是得到

Error C3646    'punch': unknown override specifier
Error C4430    missing type specifier - int assumed. Note: C++ does not support default-int

如果这是重要信息,我正在使用 sfml。

您在 hit_box.h 中定义了 hit_box,但没有使用它的文件#include <hit_box.h>编译器hit_box在编译该代码时不知道 a什么

添加到您的 player.h 文件

#include "cludes.h"
#include "hit_box.h"
class player {

“'includes.h” 是一个头文件,我将所有头文件和命名空间都放入其中。”

如果 cludes.h#includes你的其他头文件,你有循环依赖,你需要转发声明。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章