Connecting Tech Pros Worldwide Forums | Help | Site Map

"was not declarded in this scope"

Newbie
 
Join Date: Apr 2009
Posts: 6
#1: May 26 '09
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"

Expand|Select|Wrap|Line Numbers
  1. #ifndef LIB_H
  2. #define LIB_H
  3.  
  4.  
  5. #include <windows.h>
  6. #include <gl\gl.h>
  7. #include <vector>
  8. #include <string>
  9. #include <fstream>
  10.  
  11. #include <stdio.h>
  12. #include <math.h>
  13.  
  14. using namespace std;
  15.  
  16. #include "globals.h"
  17. #include "functions.h"
  18.  
  19. #include "createwindowforgl.h"
  20.  
  21. #include "bmp.h"
  22. #include "draw.h"
  23. #include "object.h"
  24. #include "world.h"
  25.  
  26.  
  27. #endif
  28.  
(I've read some where before that it is not good to use "using namespace std;" why is this?)

"world.h"

Expand|Select|Wrap|Line Numbers
  1. #ifndef WORLD_H
  2. #define WORLD_H
  3.  
  4. #include "object.h"
  5.  
  6. class WORLD
  7. {
  8.     public:        
  9.     vector<OBJECT>objects;
  10. };
  11.  
  12. #endif
  13.  
"object.h"
Expand|Select|Wrap|Line Numbers
  1. #ifndef OBJECT_H
  2. #define OBJECT_H
  3.  
  4. #include "lib.h"
  5.  
  6. class OBJECT
  7. {
  8.     struct VERTEX
  9.     {
  10.         float x, y, z;
  11.         float u, v;
  12.     };
  13.     GLuint surface;
  14.     vector<VERTEX> vertex;
  15.  
  16.     public:
  17.     OBJECT(string xSurface);
  18.     void addVertex( float x, float y, float z, float u, float v );
  19.     VERTEX getVertex(int x)
  20.     {
  21.         return vertex[x];
  22.     }
  23.     GLuint getSurface();
  24. };
  25.  
  26. #endif
  27.  
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.

Newbie
 
Join Date: Apr 2009
Posts: 6
#2: May 27 '09

re: "was not declarded in this scope"


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 ).
Member
 
Join Date: May 2009
Posts: 81
#3: May 27 '09

re: "was not declarded in this scope"


Your problem must be here:

# #ifndef WORLD_H
# #define WORLD_H
#
# #include "object.h"
#
# class WORLD
# {
# public:
# vector<OBJECT>objects;
# };
#
# #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.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#4: May 27 '09

re: "was not declarded in this scope"


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.
Newbie
 
Join Date: Apr 2009
Posts: 6
#5: May 27 '09

re: "was not declarded in this scope"


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

Expand|Select|Wrap|Line Numbers
  1. #ifndef OBJECT_H
  2. #define OBJECT_H
  3.  
  4. #include <vector>
  5. #include <string>
  6. #include <gl/gl.h>
  7.  
  8. using namespace std;
  9.  
  10. class OBJECT
  11. {
  12.     struct VERTEX
  13.     {
  14.         float x, y, z;
  15.         float u, v;
  16.     };
  17.  
  18.     GLuint surface;
  19.     vector<VERTEX> vertex;
  20.  
  21.     public:
  22.     OBJECT(string xSurface);
  23.     void addVertex( float x, float y, float z, float u, float v );
  24.     VERTEX getVertex(int x)
  25.     {
  26.         return vertex[x];
  27.     }
  28.     GLuint getSurface();
  29. };
  30.  
  31. #endif
  32.  
meaning include only what you need to use for the header?
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#6: May 28 '09

re: "was not declarded in this scope"


Yes .
Reply

Tags
c++, class, scope