【发布时间】:2017-08-02 18:16:16
【问题描述】:
我正在处理具有多个类的 CPP 项目,但我已经坚持了一段时间。我有粒子类(头文件):
#pragma once
#ifndef PARTICLE_H
#define PARTICLE_H
class Particle {
private:
vec3 pos; // <- here I get an error
vec3 dir;
float speed;
float size;
float amb[3];
public:
Particle();
Particle(bool sludge);
inline void setPosX(float posX);
inline void setPosY(float posY);
inline void setPosZ(float posZ);
inline void setSpeed(float speed);
inline void setSize(float size);
void setAmb(float amb0, float amb1, float amb2);
inline float getPosX();
inline float getPosY();
inline float getPosZ();
inline float getDirX();
inline float getDirY();
inline float getDirZ();
inline float getSpeed();
inline float getSize();
void renderParticle();
void renderSludge();
};
#endif
和 cpp 文件包括(为简单起见):
#include "stdafx.h"
#include "Tools.h"
#include "Particle.h"
#include "Player.h"
和Tools类,其中包含struct vec3:
#pragma once
#ifndef TOOLS_H
#define TOOLS_H
void distance(float posX1, float posY1, float posZ1, float radius1, float posX2, float posY2, float posZ2, float radius2);
int fibonacci(int num);
GLuint LoadObj(char * file);
GLuint LoadTexture(char * file, int magFilter, int minFilter);
void drawSphere(float r, int divisions);
void OnRender();
void OnReshape(int, int);
void OnKeyPress(unsigned char, int, int);
void OnKeyDown(unsigned char, int, int);
void OnKeyUp(unsigned char, int, int);
void OnTimer(int);
struct vec3 {
float x, y, z;
};
struct SFace {
int v[3];
int n[3];
int t[3];
};
#endif
我得到一个:Error C3646 'pos': unknown override specifier,我不明白为什么,因为Tools.h 似乎只包含一次,并且 Particle 类应该知道 Tools 包含 vec3。我尝试在 Particle 类的开头声明struct vec3,但没有成功。
【问题讨论】:
-
particle.h 如何知道 tools.h 中的内容?
-
你是用 C++11 编译的吗?
override关键字是 C++11 及更高版本的新关键字(尽管我没有在您提供的代码中看到它) -
您可能将
Particle.h包含在Particle.cpp以外的文件中。此时,Tools.h可能不包括在内。在Particle.h中包含您需要的标题。包含两次标头并没有错,特别是因为它似乎由#include和#pragma once保护。 -
同时使用编译指示和包含守卫是过大的。坚持一个。另外,养成在标题中包含标题所需的所有内容的习惯。摆脱讨厌的 setter 和 getter。最后但并非最不重要的一点是,“覆盖”从何而来?
-
我认为您不会从提供的代码 sn-p 中得到结论性的答案。请编辑您的问题以包含minimal reproducible example,假设生成 MCVE 不会使问题对您来说很明显并消除对问题的需要。如果您需要继续处理此问题,请在 MCVE 中包含您的编译器和版本号。如果您不这样做,请删除问题或更新并自行回答。