C++错误:嵌套名称说明符中使用的不完整类型

rad*_*ead 3 c++ compiler-errors incomplete-type

我有以下标头helper.h:

#ifndef ADD_H
#define ADD_H

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

#endif
Run Code Online (Sandbox Code Playgroud)

这是我的helper.cpp:

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

float Helper::calculateSpriteSize(float imgSize, float screenSize)
{
    return ((imgSize/screenSize)*100);
}
Run Code Online (Sandbox Code Playgroud)

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

#include "header.h"


int main(void){
     float h = Helper::calculateSpriteSize( 168.0f, 170.0f );
)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

错误:嵌套名称说明符中使用的不完整类型'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
Run Code Online (Sandbox Code Playgroud)

而block.cpp看起来如下:

#include "block.h"

Block::Block()
{

}

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

int Block::getImgID()
{
    return imgID;
}
Run Code Online (Sandbox Code Playgroud)

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

更新2:更改前向声明以包括.添加了我的代码中的其他包含,以防其重要.

son*_*yao 7

前向声明引入的类型是不完整类型.但是成员函数调用要求类型完整,否则编译器如何知道成员是否存在,以及它的签名?

您需要包含头文件.

#include "helper.h"

int main(void){
     float h = Helper::calculateSpriteSize( 168.0f, 170.0f );
)
Run Code Online (Sandbox Code Playgroud)

编辑

ADD_H在"block.h"和"helper.h"中使用相同的宏.这意味着

#include "block.h"
#include "helper.h"
Run Code Online (Sandbox Code Playgroud)

第二个包括失败,内容helper.h将不包括在内.

将包含保护宏名称更改为唯一,以使其更符合文件名的名称.如HELPER_HBLOCK_H.