3

I have a file that is generating the error

"Block" is not a class or a namespace name

on the line

typedef Block::point point;

However, I'm positive that it's a class that I've both created and #included in the file below

'texture.h'.

    #pragma once
#include "Block.h"
#include <iostream>
#include <vector>
#include <GL/glut.h>
#include "lodepng.h"

using namespace std;
typedef Block::point point;


class Texture
{



public:
    Texture(int width, int height, string filename);//initialises our texture and loads its pixeldata into a buffer
    Texture(void);
    ~Texture(void);

    void draw(point centerPoint, point dimensions);

    unsigned int w;//width of our imagefile
    unsigned int h;

    GLuint texID;//the ID we will give OGL for this particular texture.

private:
    vector<unsigned char> image;
    void texGLInit();
};

Also just for good measure, here is the block.h file in question.

#pragma once
#include <GL/glut.h> 
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "Texture.h"

class Block
{


public:
    struct point
    {
        GLfloat x, y;
    };

    enum State  {STATIC, FALLING, SELECTED};

    enum Colour {RED, BLUE, PURPLE, GREEN, YELLOW, DEAD};

    Block(void);
    ~Block(void);

    Block(int gridPosX, int gridPosY, point offset);
    point gridPos;//the blocks designated position in the 8x8 array
    point centerPos;//the blocks coordinates for the actual purpose of drawing

    State blockState;//the state of the block
    Colour blockColour;//the colour of the block

    void move();//checks whether the block is in a falling state and moves it appropriately
    void drawBlock();//no need to include the drawing coords as parameters. the block knows where it should be. The grid will simply tell it to draw itself.

private:
    void fall();//decrements the blocks position
    void assignRandomColour();//assigns a random colour to the block dependant upon its' texture
    void generateWorldPos();//gets our block a center position, for us to later to use to draw it.
    //the above function will also inherently define the size of each cell in the grid. I'm thinking each
    //cell should be 40x40. So each cell will be offset initially by 20px by 20px. 



    point gridOffset;



};

I have no idea as to why i could be getting this error for a class that certainly exists. Thanks in advance.

4
  • 7
    My crystal ball says "cyclic header dependency". Commented Dec 13, 2012 at 15:19
  • You're pointing to a header file, not a compilable translation unit. What file is that header included from? Commented Dec 13, 2012 at 15:20
  • Try to produce a minimal example which exhibits the problem. Commented Dec 13, 2012 at 15:20
  • You're #includeing texture.h in block.h. This means the statement typedef Block::point point; could be evaulated before you even hit the definition for Block. Try adding a forward declaration and see if that helps. Commented Dec 13, 2012 at 15:24

1 Answer 1

11

I see cyclic includes : Block.h includes Texture.h which includes Block.h.

Search for "cyclic header dependency" on this site, you will see many topics on this, where you can find solution as to how to solve this puzzle, the most common being using forward declarations of classes and pointers declarations of member data. Forward declarations of classes helps you avoid including headers, and pointers declarations of data helps you defining the enclosing class. That is a just a hint, now search for aforementioned topics for detailed answers.

Sign up to request clarification or add additional context in comments.

3 Comments

I was thinking "forward declarations", but I didn't spot the cyclical dependency. Nice one. :)
Thank you, I also saw this late. However my block.cpp actually uses instances of my Texture class. But only in the .cpp. I simply moved the include to the .cpp file and cyclical crisis averted.
@GuyJoelMcLean: You fixed that. Congrats.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.