【问题标题】:Unknown override specifier未知的覆盖说明符
【发布时间】: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 中包含您的编译器和版本号。如果您不这样做,请删除问题或更新并自行回答。

标签: c++ visual-studio-2015


【解决方案1】:

#include "Tools.h" 放在stdafx.h 中似乎可以解决问题,尽管我不明白为什么。 Tools.h 被包含在所有提到的类中(ParticlePlayer),尽管被多次包含,但它应该只包含一次,使用 #ifndef 指令。尽管如此,在我看来编译器在Particle 类中看不到Tools.h。有人可以解释为什么会发生这种情况以及为什么将标题放在stdafx.h 文件中解决了问题?

【讨论】:

  • 也许stdafx.h 中的某些内容在到达#include "Tools.h" 之前就包含了Particle.h
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 2021-08-01
  • 1970-01-01
相关资源
最近更新 更多