Patrick Muñoz Vargas Patrick Muñoz Vargas - 3 months ago 32
C++ Question

C++ error: incomplete type used in nested name specifier

I have the following header helper.h:

#ifndef ADD_H
#define ADD_H

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

#endif


This is my helper.cpp:

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

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


But, for some reason, when I call my function calculateSpriteSize on my running code by doing:

#include "header.h"


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


I get the following error:


error: incomplete type 'Helper' used in nested name specifier


Any help would be appreciated.

Block.h looks as follows:

#ifndef ADD_H
#define ADD_H

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

public:
Block();

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

#endif


And block.cpp looks as follows:

#include "block.h"

Block::Block()
{

}

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

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


UPDATE: I added Helper to the class definition as suggested by Rakete1111. This did not fix the issue though.

UPDATE 2: Changed forward declaration to include. Added other include that was in my code in case its important.

Answer

The type introduced by forward declaration is incomplete type. But member function invoking requires the type to be complete, otherwise how does the compiler know whether the member exists or not, and its signature?

You need to include the header file.

#include "helper.h"

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

EDIT

You're using the same macro ADD_H in both "block.h" and "helper.h". It means for

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

the second including would fail, the content of helper.h won't be included at all.

Change the including guard macro name to be unique, better to make it conform to the name of file name. Such as HELPER_H and BLOCK_H.