c++ 使用 class 对象作为 class 成员
c++ using a class object as a class member
"cludes.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);
}
player.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;
}
player.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
成为我可以在播放器 class 功能中调用的成员。
这些是我收到的错误消息
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
class
player.h
#include "cludes.h"
class player {
hit_box punch;
public:
player(float a, float b, int f, sf::Texture t, sf::Texture t2);
}
player.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,但 none 个文件使用它 #include <hit_box.h>
。编译器在编译该代码时不知道 hit_box
是什么。
添加到您的 player.h 文件
#include "cludes.h"
#include "hit_box.h"
class player {
"'cludes.h" 是一个头文件,我把我所有的头文件和命名空间都放在里面。"
如果cludes.h #includes
你的其他头文件,你有循环依赖,你需要forward declare.
"cludes.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);
}
player.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;
}
player.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
成为我可以在播放器 class 功能中调用的成员。
这些是我收到的错误消息
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
class
player.h
#include "cludes.h"
class player {
hit_box punch;
public:
player(float a, float b, int f, sf::Texture t, sf::Texture t2);
}
player.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,但 none 个文件使用它 #include <hit_box.h>
。编译器在编译该代码时不知道 hit_box
是什么。
添加到您的 player.h 文件
#include "cludes.h"
#include "hit_box.h"
class player {
"'cludes.h" 是一个头文件,我把我所有的头文件和命名空间都放在里面。"
如果cludes.h #includes
你的其他头文件,你有循环依赖,你需要forward declare.