"was not declarded in this scope"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sfuo
    New Member
    • Apr 2009
    • 6

    "was not declarded in this scope"

    Hey guys, I went from working with SDL to OpenGL now and have transferred some code from one project to another. I do not understand why I am getting an error with this:

    "lib.h"

    Code:
    #ifndef LIB_H
    #define LIB_H
    
    
    #include <windows.h>
    #include <gl\gl.h>
    #include <vector>
    #include <string>
    #include <fstream>
    
    #include <stdio.h>
    #include <math.h>
    
    using namespace std;
    
    #include "globals.h"
    #include "functions.h"
    
    #include "createwindowforgl.h"
    
    #include "bmp.h"
    #include "draw.h"
    #include "object.h"
    #include "world.h"
    
    
    #endif
    (I've read some where before that it is not good to use "using namespace std;" why is this?)

    "world.h"

    Code:
    #ifndef WORLD_H
    #define WORLD_H
    
    #include "object.h"
    
    class WORLD
    {
    	public:		
    	vector<OBJECT>objects;
    };
    
    #endif
    "object.h"
    Code:
    #ifndef OBJECT_H
    #define OBJECT_H
    
    #include "lib.h"
    
    class OBJECT
    {
    	struct VERTEX
    	{
    		float x, y, z;
    		float u, v;
    	};
    	GLuint surface;
    	vector<VERTEX> vertex;
    	
    	public:
    	OBJECT(string xSurface);
    	void addVertex( float x, float y, float z, float u, float v );
    	VERTEX getVertex(int x)
    	{
    		return vertex[x];
    	}
    	GLuint getSurface();
    };
    
    #endif
    So the errors that it shows are:

    'OBJECT' was not declared in this scope
    ISO C++ forbids declaration of 'objects' with no type

    I know the 2nd error there is because vector<OBJECT> is getting messed up so it doesn't have a type.

    Any help with this would be great because I have been looking over this for a while now and it is just making me angry =(.

    Thanks.
  • sfuo
    New Member
    • Apr 2009
    • 6

    #2
    turns out in my "object.cpp " file I was including "object.h" and I was the source of the problem. I changed this to "lib.h" ( I do not fully understand why this fixed it seeing how in object.h lib.h is included, this probably shows some of the advanced C++ guys how "not smart" I am when it comes to this ).

    Comment

    • unauthorized
      New Member
      • May 2009
      • 81

      #3
      Your problem must be here:

      # #ifndef WORLD_H
      # #define WORLD_H
      #
      # #include "object.h"
      #
      # class WORLD
      # {
      # public:
      # vector<OBJECT>o bjects;
      # };
      #
      # #endif

      [edited, see bellow]

      As for "using namespace std;", there is nothing wrong with it. Most C++ libraries avoid putting it in headers, simply because some people with non-standard 15 years old C++ code still have classes and functions that conflict with the definitions inside std::.
      Unless you are writing a general purpose library meant for wide distribution that may include ancient code, you don't have to concern yourself with using it.

      edit:
      Turns out I was incorrect. Your compiler is getting confused when recursively including "object.h" from "lib.h". It may have something to do with how the preprocessor directives are being handled.
      A possible solution is to move #include "object.h" from lib.h to the actual header(s) that need it.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        This is a common problem when A.h includes B.h and then B.h includes A.h as is the case here with lib.h and object.h

        When OBJECT.h is included it sets its include protection symbol OBJECT_H. It then includes LIB.h before declaring class OBJECT.

        When LIB.h includes OBJECT.h OBJECT_H is already set so all of the contents of OBJECT.h are ignored.

        LIB.h then includes WORLD.h but note class OBJECT is not yet declared because in the first inclusion of OBJECT.h we have only reached the include LIB.h line and in the second inclusion of OBJECT.h we ignored the entire file so class WORLD is declared without class OBJECT being declared and the error occurs.

        This more commonly happens when class A uses class B and class B uses class A. The solution there is to replace one of the includes with a forward declaration and make one of the classes only use references to the other in its declaration. For example if A only uses references to B then you can forward declare B in A.h rather than including B.h and in B.h you can safely include A.h.

        However in this case there is no interaction like that between classes it is merely how the headers are include. The solution is not to include LIB.h into OBJECT.h, if OBJECT.h needs other headers then it should explicitly include them. It is almost always the case that if you have a header that includes all the other headers to ease inclusion order you should not include that overall header into any of your normal headers.

        You should never include A from B when A already includes B.

        Comment

        • sfuo
          New Member
          • Apr 2009
          • 6

          #5
          So you are saying that for something like my object header I should change it to:

          Code:
          #ifndef OBJECT_H
          #define OBJECT_H
          
          #include <vector>
          #include <string>
          #include <gl/gl.h>
          
          using namespace std;
          
          class OBJECT
          {
          	struct VERTEX
          	{
          		float x, y, z;
          		float u, v;
          	};
          
          	GLuint surface;
          	vector<VERTEX> vertex;
          	
          	public:
          	OBJECT(string xSurface);
          	void addVertex( float x, float y, float z, float u, float v );
          	VERTEX getVertex(int x)
          	{
          		return vertex[x];
          	}
          	GLuint getSurface();
          };
          
          #endif
          meaning include only what you need to use for the header?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Yes .

            Comment

            Working...