gpt4 book ai didi

c++ - #include混淆

转载 作者:搜寻专家 更新时间:2023-10-31 00:19:30 26 4
gpt4 key购买 nike

为了避免出现包含循环,我将之前在类 header 中的#include 替换到了它的 cpp 文件中。然后我在标题中放置了一个前向声明。

这解决了我的问题,但是,源文件似乎不再能够访问 cpp 文件中包含文件的成员。为什么是这样?什么是解决方案?

编辑 - 下面的真实代码:

标题:

#ifndef BULLET_H
#define BULLET_H

#include "BoundingSphere.h"
#include "helperMethods.h"

class GameObject;

class Bullet : public GameObject
{
private:
float velocity;
float distance;
D3DXVECTOR3 firedFrom;
BoundingSphere bSphere;

public:
Bullet(D3DXVECTOR3 position, D3DXVECTOR3 rotation, float velocity, D3DXCOLOR colour);
BoundingSphere BulletSphere();//change this to simply use position
void Update();
};

#endif

来源:

#include "Bullet.h"
#include "GameObject.h"

Bullet::Bullet(D3DXVECTOR3 position, D3DXVECTOR3 rotation, float velocity, D3DXCOLOR colour)
: bSphere(BoundingSphere(position, 0.01f))
{
//Model = content.Load<Model>("Models\\basicMesh");
//Texture = content.Load<Texture2D>("Textures\\basicMesh");

distance = 0.0f;

Scale(D3DXVECTOR3(0.25f, 0.1f, 0.1f));// error

Colour(colour); //error

firedFrom = position;

Rotation() = rotation; // error
this->velocity = velocity;
}

BoundingSphere Bullet::BulletSphere()
{
return bSphere;
}

void Bullet::Update()
{
Position(D3DXVECTOR3Helper.PointOnXYCircle(firedFrom, distance, Rotation().z)); //error
bSphere.Centre(Position());
distance += velocity;
}

第一个错误是 GameObject: base class undefined。

所有后续错误都是“错误:标识符“whatever”未定义。它们都是公共(public)的和游戏对象中的 setter/getter 。

最佳答案

你一定是事先得到一个编译器错误,前向声明不允许你从一个类继承。请发布您的代码。

下面一行:

class Bullet : public GameObject

意味着您必须在 Bullet.h 中包含 GameObject.h。同样,前向声明不足以继承。如果您遇到更多错误,请发布确切的错误代码、文本和行号(在您进行更改后)。

关于c++ - #include混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8050316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com