【问题标题】:C++ error: incomplete type used in nested name specifierC++ 错误:嵌套名称说明符中使用的类型不完整
【发布时间】:2016-08-09 03:09:49
【问题描述】:

我有以下头文件 helper.h:

#ifndef ADD_H
#define ADD_H

class Helper{
public:
    static float calculateSpriteSize(float imgSize, float screenSize);
};

#endif

这是我的 helper.cpp:

#include "block.h"
#include "helper.h"

float Helper::calculateSpriteSize(float imgSize, float screenSize)
{
    return ((imgSize/screenSize)*100);
}

但是,由于某种原因,当我在正在运行的代码上调用我的函数 calculateSpriteSize 时:

#include "header.h"


int main(void){
     float h = Helper::calculateSpriteSize( 168.0f, 170.0f );
)

我收到以下错误:

错误:嵌套名称说明符中使用了不完整的类型“Helper”

任何帮助将不胜感激。

Block.h 如下所示:

#ifndef ADD_H
#define ADD_H

class Block{
    private:
         int imgID;
         int life;
         float price;

    public:
         Block();

         void setImgID(int imgID);
         int getImgID();
    };

    #endif

而block.cpp如下:

#include "block.h"

Block::Block()
{

}

void Block::setImgID(int imgID)
{
     this->imgID = imgID;
}

int Block::getImgID()
{
    return imgID;
}

更新:我按照 Rakete1111 的建议将 Helper 添加到类定义中。但这并没有解决问题。

更新 2:将前向声明更改为包含。添加了其他包含在我的代码中的内容,以防它很重要。

【问题讨论】:

  • 我不认为这是原因,但不应该是float Helper::calculateSpriteSize(...) { ... }吗?
  • 糟糕。你说的对。我修好了。但这并没有解决问题。同样的错误仍然存​​在
  • helper.h 中是否包含任何文件?
  • 如果你唯一的类是 'helper' 并且它没有引用任何其他声明,那么 'nested...' 错误没有意义。你能发布整个源代码吗?
  • 您是否有一个 Add.h 标头也使用预处理器符号 ADD_H 作为其包含防护?不要浪费大家的时间,包括你的,玩这个猜谜游戏,请发帖minimal reproducible example

标签: c++ compiler-errors incomplete-type


【解决方案1】:

前向声明引入的类型是incomplete type。但是成员函数调用需要类型完整,否则编译器怎么知道成员是否存在,以及它的签名呢?

你需要包含头文件。

#include "helper.h"

int main(void){
     float h = Helper::calculateSpriteSize( 168.0f, 170.0f );
)

编辑

您在“block.h”和“helper.h”中使用了相同的宏 ADD_H。这意味着

#include "block.h"
#include "helper.h"

第二次包含失败,helper.h 的内容根本不会包含。

将包含的保护宏名称更改为唯一,最好使其符合文件名的名称。比如HELPER_HBLOCK_H

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    相关资源
    最近更新 更多