RE: Design orientated 'C' question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Robby

You *must* include both times, each CPP file compiles independently,
generating one .OBJ file for each of them, then the linker is who links .OBJ
compiled functions resolving names as addresses and generating the .EXE... If
you dont include "globals.h" on each CPP file you are *not declaring* what is
in globals for that CPP file.

I hope this helps
Regards

"Robby" wrote:

Sorry,

In my above post the following line:

//Function declaration
void L1_txtDISPLAY_ArSt2MEMB( HDC, struct tagIdRoomIndex RIndex[] );

should of been:

//Function declaration
void F1( HDC, struct tagIdRoomIndex RIndex[] );

--
Best regards
Robert


"Robby" wrote:

Hi,

Please consider the following code:

Globals.h
=================================================
//Declaration of structure
struct tagIdRoomIndex
{
int iRoomNumber;
TCHAR *szRoomName;
};

=================================================

WndProc_CW1.cpp
==================================================
#include <windows.h>
#include "Globals.h"

//Function declaration
void L1_txtDISPLAY_ArSt2MEMB( HDC, struct tagIdRoomIndex RIndex[] );

//Declaration of an array of structures
tagIdRoomIndex RIndex[] =
{
1 ,TEXT("ABC"),
2 ,TEXT("DEF"),
3 ,TEXT("GHI"),
4 ,TEXT("JKL"),
};


LRESULT CALLBACK WndProc_CW1 (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch(message)
{
WM_PAINT:
F1( hdc,RIndex); //Calling function
break;
}
//Other code....
}
==================================================

LIB1_FUNCTIONS.cpp (Function implementations)
==================================================
#include <windows.h>

//#include "Globals.h" //Already included in WndProc.cpp

F1(HDC hdc,struct tagIdRoomIndex RIndex[])
{
//Other code ...

//Errors point to the < RIndex[i].iRoomNumber)); > part of line below
TextOut (hdc,0,0,szBuffer,
wsprintf(szBuffer,TEXT("%d"),RIndex[i].iRoomNumber));

}
==================================================

This code compiles OKAY if I include the following line of code in the
function implementation file called LIB1_FUNCTIONS.cpp

#include "Globals.h"

However if I don't include "Globals.h" I get several errors. Here are 2 of
them:

c:\_DTS_PROGRAMMING\vc++\MY_APPS_LAB\APPLICATION_1\LIB1_FUNCTIONS.cpp(74) :
error C2036: 'tagIdRoomIndex []' : unknown size

c:\_DTS_PROGRAMMING\vc++\MY_APPS_LAB\APPLICATION_1\LIB1_FUNCTIONS.cpp(74) :
error C2027: use of undefined type 'tagIdRoomIndex'

My question is, when I am passing the array of structures by the calling
function:

F1(hdc,RIndex);

Am I not passing the array and the structure. I have tried the following
ways to pass the array of structures in the calling function:

F1( hdc,RIndex); //Calling function

OR

F1( hdc,tagIdRoomIndex RIndex); //Calling function

And I always get errors.....

I find it redundant if I have to include the "Globals.h" once in WndProc_CW1
and once again in LIB1_FUNCTIONS.cpp

I don't mind, I can include it twice! *I could live with this* However Isn't
this bad coding habits?

As far as where my declarations take place, I believe that declaring a
structure in a header would not really be a bad idea, right...I mean, I have
seen it in book samples!
But then again, they were not books that showed serious examples under the
context dealing with structuring of programming.

I would appreciate all feedback on this issue... Thankyou all.

All suggestions are appreciated

--
Best regards
Robert
.



Relevant Pages

  • Re: Array definition and extern declaration with different size.
    ... I have an array defined in one file with an intializer as follows: ... This compiles without a problem on my implementation and arr ends up ... Any size specified in the external declaration is ignored by the ... "For two array types to be compatible, ...
    (comp.lang.c)
  • Re: Quick question!
    ... class components; // forward declaration ... error C2065: 'components': undeclared identifier ... Can we pass a pointer to an object from one cpp file to another by using ...
    (microsoft.public.vc.language)
  • Re: Newbie making a roguelike
    ... which references my hard-coded dungeon ... file and they can't reference the global dungeon array. ... Always declare classes and structs and such in your .h/.hpp files. ... write the implementation in the .cpp file. ...
    (rec.games.roguelike.development)
  • Re: Weird... std::vector::push_back exception
    ... not pointers. ... So, again, in my header I declare the vector, and then in my cpp file ... The weird part is that if I cut & paste EXACTLY the same declaration ... from both the header file and the .cpp file would help. ...
    (microsoft.public.vc.stl)
  • Re: Errors when compiling a VC6 project in VC2005
    ... all the other errors in *.CPP file: ... CObject isn't copyable, so it is not suitable as element in a container. ... \afx.h: see declaration of 'CObject' ... typedef is likely to conflict with that. ...
    (microsoft.public.vc.language)