3 Replies - 776 Views - Last Post: 12 June 2012 - 12:24 PM Rate Topic: -----

#1 notinasia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-June 12

What is wrong with my code organization?

Posted 11 June 2012 - 04:38 PM

First off, let me introduce you to the method of code organization I've been using. I'll have one main .cpp file where the basic functionality of the entire program can be understood. High-level functions called by the main .cpp file are placed in a .h file. Low-level functions called by the high-level functions are placed in other .h files and so on.

No compiler problems until I get to the issue of struct/object recognition among different .h files.

I've encountered problems with global structs being recognized in some compiled .h files, but not in others.

for example:

#ifndef low_level1
#define low_level1

struct my_struct
{
  int x;
  int y;
} its_mine;

#endif




Now, if I have several low-level .h files that use its_mine, and I #include "low_level1" in all of these other .h files, eventually I end up with compiler errors like:

error C2065: 'its_mine' : undeclared identifier



when some of the .h files are being compiled. Now I know what the error message means, it just means that the declaration of its_mine isn't found for some of the .h files even though I used #include "low_level1.h". This doesn't make sense to me why.

I temporarily fixed this by making all my globally used structs into objects and placing them all in an objects.h.

Now I have

#ifndef objects
#define objects
class my_class
{
public:
    int x;
    int y;
};
my_class its_mine;

#endif



I used #include "objects.h" in all of the .h files that used these objects and this worked for long enough that I thought that this was a solution to my woes. However, as my project grew I ended up with the same problem.

This isn't a method of code organization I learned from someone, it's just one I've used to serve my needs but I don't understand why compilers (I'm using VS2008) don't like it.

So my question is what is wrong with my code organization (besides the obvious answer everything)?

In case this isn't a question that can be answered without an exact example (some people make it sound like no question is) here are some snippets (I really hope no one thinks it's necessary to ask for all 10+ files and 5000+ lines of code...)


#ifndef objects
#define objects

class Squares{

int member;

};

#define SquaresY 13
#define SquaresX 6
Squares Square[SquaresY][SquaresX];

#endif




#ifndef image
#define image

#include "objects.h"
#include "low_level1.h"
#include "low_level2"

if((Square[0][0].member != 0)
{
;
}
#endif




sample error:

c:\visual studio\....\image.h(33) : error C2065: 'Square': undeclared identifier



wheras Square is recognized as declared in several other .h files:
low_level1.h,
low_level2.h, etc.

and:

#ifndef low_level1
#define low_level1

#include "objects.h"
#include "image.h"
#include "low_level2.h"

// Compiler recognizes Square
// Code that uses Square....

#endif



#ifndef low_level2
#define low_level2

#include "objects.h"
#include "image.h"
#include "low_level1.h"

// Compiler recognizes Square
// Code that uses Square....

#endif



Is This A Good Question/Topic? 0
  • +

Replies To: What is wrong with my code organization?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,421
  • Joined: 25-December 09

Re: What is wrong with my code organization?

Posted 11 June 2012 - 04:47 PM

In the following snippet:
#ifndef image
#define image

#include "objects.h"
#include "low_level1.h"
#include "low_level2"

if((Square[0][0].member != 0)
{
;
}
#endif



You can't have an if statement outside a function.

Next question. Why are you trying to use global variables (class/struct instances)? Keep your variables local to your functions. If you need a variable in more than one function pass it into the functions that require it.

Unless you are working with templates, functions should be placed in separate .cpp files, not in .h files.

Jim

This post has been edited by jimblumberg: 11 June 2012 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 notinasia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-June 12

Re: What is wrong with my code organization?

Posted 12 June 2012 - 11:19 AM

Right right, silly me with the if outside of the function example I'll edit it, but thats not the real issue, or why I posted this thread.

So, why do I get even more compiler errors when I place Squares[][] in object.cpp??

#ifndef objects
#define objects

#include objects.cpp

class Squares
{
...
...
};

#endif



objects.cpp:
#include objects.h

#define SquaresY 13
#define SquaresX 6

Squares Square[SquaresY][SquaresX];



c:\......\objects.cpp(6) : error C2146: syntax error : missing ';' before identifier 'Square'


Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,421
  • Joined: 25-December 09

Re: What is wrong with my code organization?

Posted 12 June 2012 - 12:24 PM

Never "include" .cpp files. Add the file to your project.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1