When I execute this code, I get the following error: ‘Block’ does not name a type This is the code:

Block.h

#include "Area.h"
#ifndef Block_H
#define Block_H
#ifndef Cube_H
#include "Cube.h"
#endif
#ifndef nullptr
#define nullptr 0
#endif

class Block : public Area
{

    int ID;
    int BlockType;
    Cube *cube;
    Block()
    {
    ID=0;
    BlockType=0;
    cube=0;
    }
};
#endif

Cube.h

#include "Area.h"
#ifndef Cube_H
#define Cube_H
#ifndef Block_H
#include "Block.h"
#define Block_H
#endif
// put all your classes and other stuff here
class Cube : public Area
{
    Block *blocks[8]=nullptr;
    int ID;
};

#endif // end of MYHEADER_H

this code, for whatever reason, doesn't compile. I've cross checked it with other sources, and it still doesn't compile.

Thanks.

Recommended Answers

All 3 Replies

Try class Block * blocks[8] = nullptr; in Cube.h and/or class Cube *cube; in Block.h

I believe you need to forward declare one of your classes.

On including cube.h and compiling block.h you would get "Type name expected" for block. The cube.h will be compiled first before the compilation of class block.

class Cube : public Area
{
    Block *blocks[8]=nullptr;
    int ID;
};

Only after that the class block gets compiled.

class Block : public Area
{
 
    int ID;
    int BlockType;
    Cube *cube;
    Block()
    {
    ID=0;
    BlockType=0;
    cube=0;
    }
};

If you include block.h then also it would say "cube does not name a type".
So you have to declare the classes accordingly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.