473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

"was not declarded in this scope"

6
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.
May 26 '09 #1
5 2310
sfuo
6
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 ).
May 27 '09 #2
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.
May 27 '09 #3
Banfa
9,065 Expert Mod 8TB
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.
May 27 '09 #4
sfuo
6
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?
May 27 '09 #5
Banfa
9,065 Expert Mod 8TB
Yes .
May 28 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: William | last post by:
In Peer.h, I have: class Peer { // ... }; In Overseer.h, I have: #include "Peer.h" #include <vector>
3
by: Luigi Donatello Asero | last post by:
Is an index in a database the equivalent for a <TH scope="col"> in a column of a table in the html code? -- Luigi ( un italiano che vive in Svezia)...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
1
by: G Fernandes | last post by:
Scope is a property of identifiers and defines where they are visible in a source file. Why then do most writings on C also use the word "scope" to refer to a property of different points of a...
3
by: marco_segurini | last post by:
Hi, I am using VS 2005. If I compile the following code only line 6 returns me an error while line 9 returns a warning. If I comment the line 6 and debug the program the assignments of lines...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
39
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the...
2
by: Bryan | last post by:
Hello all, Can anyone explain when one should use the "document" object and when one should use the "this" object? Also, is the "self" object the same as the "document" or "this" object?
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.