Problems with multiple CPP files in my project

D

Daniel Moree

I'm using MS VC++ 6.0
I'm working on a big project. I've currently have several files for this
project.

Here's the problem.
I have one header file phead.h
I have two code files main.cpp and gameloop.cpp

phead.h has all my core declarations in it like my main globals.
main.cpp has all my window initilization functions and my winproc loop.
gameloop.cpp has my main game loop, this file uses phead.h declarations and
hold HWND and WPARAM for keyboard control and window contols.

When I compile and run, sometimes it will compile and run fine. Then, when I
edit some of the gameloop.cpp file or the main.cpp file trying something
different, it won't compile because it says it doesn't know what HWND and
WPARAM are. I include it in gamemain.cpp and the compiler says there are
multiple statements for my gamemain.cpp functions, then i can delete the
include command and it will work, any idea on how to fix it or ideas on
project organization?
 
K

Karthik Kumar

Daniel said:
I'm using MS VC++ 6.0
I'm working on a big project. I've currently have several files for this
project.

Here's the problem.
I have one header file phead.h
I have two code files main.cpp and gameloop.cpp

phead.h has all my core declarations in it like my main globals.
main.cpp has all my window initilization functions and my winproc loop.
gameloop.cpp has my main game loop, this file uses phead.h declarations and
hold HWND and WPARAM for keyboard control and window contols.

When I compile and run, sometimes it will compile and run fine. Then, when I
edit some of the gameloop.cpp file or the main.cpp file trying something
different, it won't compile because it says it doesn't know what HWND and
WPARAM are. I include it in gamemain.cpp

What is 'it' here ? How do you include the same ?
Do you mean to say you include specific headers that
has the relevant type definitions ?
and the compiler says there are
multiple statements for my gamemain.cpp functions, then i can delete the
include command and it will work, any idea on how to fix it or ideas on
project organization?

Have you used header guards in your project ?
With the little info. that you had provided it seems like
that could be the cause of the problem.
 
S

Sharad Kala

Daniel Moree said:
I'm using MS VC++ 6.0
I'm working on a big project. I've currently have several files for this
project.

Here's the problem.
I have one header file phead.h
I have two code files main.cpp and gameloop.cpp

phead.h has all my core declarations in it like my main globals.
main.cpp has all my window initilization functions and my winproc loop.
gameloop.cpp has my main game loop, this file uses phead.h declarations and
hold HWND and WPARAM for keyboard control and window contols.

When I compile and run, sometimes it will compile and run fine. Then, when I
edit some of the gameloop.cpp file or the main.cpp file trying something
different, it won't compile because it says it doesn't know what HWND and
WPARAM are. I include it in gamemain.cpp and the compiler says there are
multiple statements for my gamemain.cpp functions, then i can delete the
include command and it will work, any idea on how to fix it or ideas on
project organization?

You will get more than just speculative answers if you post the _minimal_
code that shows your problem.

Sharad
 
J

John Harrison

Daniel Moree said:
I'm using MS VC++ 6.0
I'm working on a big project. I've currently have several files for this
project.

Here's the problem.
I have one header file phead.h
I have two code files main.cpp and gameloop.cpp

phead.h has all my core declarations in it like my main globals.
main.cpp has all my window initilization functions and my winproc loop.
gameloop.cpp has my main game loop, this file uses phead.h declarations
and
hold HWND and WPARAM for keyboard control and window contols.

When I compile and run, sometimes it will compile and run fine. Then, when
I
edit some of the gameloop.cpp file or the main.cpp file trying something
different, it won't compile because it says it doesn't know what HWND and
WPARAM are. I include it in gamemain.cpp and the compiler says there are
multiple statements for my gamemain.cpp functions, then i can delete the
include command and it will work, any idea on how to fix it or ideas on
project organization?

Never include one cpp file in other.

Learn what should go in header file and what shouldn't. The list is too long
to repeat here, consult a good C++ book, but the biggest newbie mistake is
too define global variables in header file. You say you are declaring global
variables, that is OK, but many newbies don't know the difference between a
definition and a declaration, so maybe you have it wrong.

Every header file should include the header files it need to compile. So,
suppose your header file mentions HWND and WPARAM, then you header file
should include <windows.h>

Always use include guards in header files, and understand what they do and
don't do.

Don't make random changes to try and fix errors you don't understand. That
will just end up confusing you and any lessons learned are likely to be
incorrect.

When you have problems with code post the code.

Accept that solving your problems may take a few iterations.

Read good C++ books where all this stuff is explained.

That's the best I can do based on the small amount of information provided.
Be a bit more specific and I'll try and help some more.

john
 
D

Daniel Moree

Ok, Let me clear this up. Sorry for the short description.

phead.h - contains all global information, AppName/Class, Window size, and
Window depth, and two functions, fnKeyDown(GetAsyncKeyState) and
fnKeyUp(GetAsyncKeyState)

main.cpp - contains all header files to include such as, windows.h,
windowsx.h, stdio.h, math.h, stdlib.h all withing "#include <file>"
brackets. I've included below that the following:

#include "phead.h"
#include "gameloop.cpp"

main.cpp is used to start the program with WindowProc and WinMain in it.
These are the only two functions in this file. The file defines
WIN32_LEAN_AND_MEAN to exclude MFC.

gameloop.cpp - has the main game loop aswell as a key handler that uses
WM_KEYDOWN instead of using GetAsyncKeyState(). Two functions, GameLoop(HWND
hWnd) and KeyHandler(WPARAM wParam).

The problem I'm having is that when the program is compiled it will work
fine one time and then I could add another If statement to my GameLoop()
function, and then compile which will cause errors saying it can't find what
HWND are and WPARAM are. I delete the if statement and it still does it. So
I #include <windows.h> and it says "fnKeyDown" is and undeclared Identifier.
I #include "phead.h" and compile and it says multiple statements of my
functions in gameloop.cpp. I delete both #include statements and sometimes
it will work again and sometimes it won't.

My gameloop.cpp file has at the top
#ifndef GAMELOOP
#define GAMELOOP
and at the end i've added the #endif, the same for phead.h but with the name
PHEAD.
My main.cpp does not have this. I've tried moving my include statments to
the Phead.h file so they are global but then main.cpp doesn't work. Not sure
what to do here so all help is greatly apprciated!!!
 
J

John Harrison

Daniel Moree said:
Ok, Let me clear this up. Sorry for the short description.

phead.h - contains all global information, AppName/Class, Window size, and
Window depth, and two functions, fnKeyDown(GetAsyncKeyState) and
fnKeyUp(GetAsyncKeyState)

main.cpp - contains all header files to include such as, windows.h,
windowsx.h, stdio.h, math.h, stdlib.h all withing "#include <file>"
brackets. I've included below that the following:

#include "phead.h"
#include "gameloop.cpp"

That is wrong. As I said *never* include one cpp file inside another.
main.cpp is used to start the program with WindowProc and WinMain in it.
These are the only two functions in this file. The file defines
WIN32_LEAN_AND_MEAN to exclude MFC.

gameloop.cpp - has the main game loop aswell as a key handler that uses
WM_KEYDOWN instead of using GetAsyncKeyState(). Two functions, GameLoop(HWND
hWnd) and KeyHandler(WPARAM wParam).

The problem I'm having is that when the program is compiled it will work
fine one time and then I could add another If statement to my GameLoop()
function, and then compile which will cause errors saying it can't find what
HWND are and WPARAM are. I delete the if statement and it still does it. So
I #include <windows.h> and it says "fnKeyDown" is and undeclared Identifier.
I #include "phead.h" and compile and it says multiple statements of my
functions in gameloop.cpp. I delete both #include statements and sometimes
it will work again and sometimes it won't.

My gameloop.cpp file has at the top
#ifndef GAMELOOP
#define GAMELOOP

That is wrong, include guards are for header file, not source files.
and at the end i've added the #endif, the same for phead.h but with the name
PHEAD.
My main.cpp does not have this. I've tried moving my include statments to
the Phead.h file so they are global but then main.cpp doesn't work. Not sure
what to do here so all help is greatly apprciated!!!

As I said, don't make random changes when you don't understand what you are
doing. You will just end up in an even worse mess than you are now.

Now I have told you two things wrong, fix them. You probably still have lots
of other things wrong so it isn't going to work straightaway. Post again
we'll get there eventually, put please post the code, not descriptions of
code.

You could do worse than post the contents of phead.h

john
 
D

Daniel Moree

I've fixed these problems and tried several different things. My code if
fine, no errors at all. The code works greate all in one file with phead.h
the same way. I'm splitting up my commands to different files to make it
easier to edit. Below are my files:

===== phead.h ======
#ifndef PHEADER
#define PHEADER
// Main Application Constants
#define g_sAppName "Paint It: Paintball" // What the title will read
#define g_sAppClass "PaintIt" // What windows know the window as
// Window variables
int g_nVMode = 1; // 0 - 640x480, 1 - 800x600, 2 - 1024x768
int g_aWindowWidth[4] = {640, 800, 1024}; // Width: 640, 800, 1024
int g_aWindowHeight[4] = {480, 600, 768}; // Height: 480, 600, 768
int g_aWindowDepth[5] = {8, 16, 24, 32}; // Depth: 8, 16, 24, 32
// Window Globals
bool g_bQuitMsg = false; // This tells the Window to close or not
// Key states
#define fnKeyDown(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define fnKeyUp(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#endif

===== main.cpp =====
#define WIN32_LEAN_AND_MEAN
/***********************************************/
/* Include Files */
/***********************************************/
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <math.h>
#include <mmsystem.h>
#include <stdlib.h>

#include "phead.h"

/***********************************************/
/* Windows Procedure */
/***********************************************/
LRESULT CALLBACK WindowProc(HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam){
switch(msg) {
case WM_CREATE:
{
// Initialization Commands

} break;

case WM_PAINT:
{
// Paint commands
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hWnd, &ps);

EndPaint(hWnd, &ps);

} break;

case WM_DESTROY:
{
// Close commands
g_bQuitMsg = true;

} break;

case WM_QUIT:
{
g_bQuitMsg = true;
} break;

case WM_KEYDOWN:
{
int iKeyCode = wParam;
KeyHandler(iKeyCode);
} break;
}

return DefWindowProc(hWnd, msg, wParam, lParam);
}

/***********************************************/
/* WinMain */
/***********************************************/
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR szCmdLine,
int iCmdShow){
WNDCLASSEX winclass; // Holder for the window class
HWND hWnd; // Window Handle
MSG msg; // Message holder

winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hInstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = g_sAppClass;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// Register the window
if(!RegisterClassEx(&winclass)){
Error(NULL, "Fatal Error Registering App!");
return 0;
}

// Create the window
hWnd = CreateWindowEx(NULL, // Extended Style
g_sAppClass, // Window class name
g_sAppName, // Window caption
WS_OVERLAPPEDWINDOW, // Window style
0, // Initial x position
0, // Initial y position
g_aWindowWidth[g_nVMode], // Initial window width
g_aWindowHeight[g_nVMode],// Initial window height
NULL, // Parent window handle
NULL, // Window menu handle
hInstance, // Program instance handle
NULL); // Creation Parameters

if(!hWnd){
Error(NULL, "Failed To Create Window!");
return 0;
}

ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);

while(g_bQuitMsg == false){
if(PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)){
if(msg.message == WM_QUIT){ g_bQuitMsg = true; }
TranslateMessage(&msg);
DispatchMessage(&msg);
}
MainLoop(hWnd);
}
return(msg.wParam);
}

===== gameloop.cpp =====
/***********************************************/
/* Game Functions */
/***********************************************/
void MainLoop(HWND hWnd);
void KeyHandler(WPARAM wParam);
void Error(HWND hWnd, LPCTSTR strMsg);

/***********************************************/
/* Main Loop */
/***********************************************/
void MainLoop(HWND hWnd){
if(fnKeyDown(VK_DOWN)){
Error(NULL, "Down!");
}
}

/***********************************************/
/* Error Message Center */
/***********************************************/
void Error(HWND hWnd, LPCTSTR strMsg){
MessageBox(hWnd, strMsg, "Error!", MB_OK | MB_ICONWARNING);
}

/***********************************************/
/* Key Press Handler */
/***********************************************/
void KeyHandler(WPARAM wParam){
switch(wParam){
case VK_ESCAPE:
{ SendMessage(NULL, WM_DESTROY, 0, 0); }
break;

case VK_UP:
{ Error(NULL, "You pressed the up key!"); }
break;
}
}
 
J

John Harrison

Daniel Moree said:
I've fixed these problems and tried several different things. My code if
fine, no errors at all. The code works greate all in one file with phead.h
the same way. I'm splitting up my commands to different files to make it
easier to edit. Below are my files:

===== phead.h ======
#ifndef PHEADER
#define PHEADER
// Main Application Constants
#define g_sAppName "Paint It: Paintball" // What the title will read
#define g_sAppClass "PaintIt" // What windows know the window as
// Window variables
int g_nVMode = 1; // 0 - 640x480, 1 - 800x600, 2 - 1024x768
int g_aWindowWidth[4] = {640, 800, 1024}; // Width: 640, 800, 1024
int g_aWindowHeight[4] = {480, 600, 768}; // Height: 480, 600, 768
int g_aWindowDepth[5] = {8, 16, 24, 32}; // Depth: 8, 16, 24, 32
// Window Globals
bool g_bQuitMsg = false; // This tells the Window to close or not


These globals are all wrong.

The function prototypes for gameloop.cpp are missing.

Various other problems.


Here is how you should do it

1) Globals definitions go in a cpp file, say main.cpp, so put this in
main.cpp

// Window variables
int g_nVMode = 1; // 0 - 640x480, 1 - 800x600, 2 - 1024x768
int g_aWindowWidth[4] = {640, 800, 1024}; // Width: 640, 800, 1024
int g_aWindowHeight[4] = {480, 600, 768}; // Height: 480, 600, 768
int g_aWindowDepth[5] = {8, 16, 24, 32}; // Depth: 8, 16, 24, 32

// Window Globals
bool g_bQuitMsg = false; // This tells the Window to close or not

2) Global declarations go in the phead.h file. So put this in phead.h

extern int g_nVMode;
extern int g_aWindowWidth[4];
extern int g_aWindowHeight[4];
extern int g_aWindowDepth[5] ;

// Window Globals
extern bool g_bQuitMsg;

3) When you said you had put the global declarations in the header file you
were wrong. You had put global variable definitions in the header file,
that is wrong. Many newbies do not understand the difference between a
declaration and a definition, but it is crucial to understand the difference
if you are going to work with more than one source file.

4) You need to put function prototypes in phead.h for the function you one
to call from one cpp file but are defined in another cpp file. Add this to
phead.h

void MainLoop(HWND hWnd);
void KeyHandler(WPARAM wParam);
void Error(HWND hWnd, LPCTSTR strMsg);

And delete the same from gameloop.cpp

5) phead.h now mentions windows types, so add #include <windows.h> near the
top of phead.h

6) phead.h declares what is common between main.cpp and gameloop.cpp, so
include it in both cpp files. At the moment you are just including it in
main.cpp

7) Do all the above and you will be a lot closer. I strongly suspect there
are other issues though, so post again if you get stuck. You really should
buy a book on C++, what you had was an awful long way from being correct.

john
 
D

Daniel Moree

7) Do all the above and you will be a lot closer. I strongly suspect there
are other issues though, so post again if you get stuck. You really should
buy a book on C++, what you had was an awful long way from being correct.

john

I've got several, I've been programming C/C++ for a while now and i'm just
moving to multiple files. I've got C++ Primer Plus, Teach yourself C in 24
hours, and a few others. I've always had my programs working great. My
program will work fine and I've had it working fine before. Why it doesn't
work now I don't know. This is the same setup I've had for a few other
programs in several files although the main code was in one file and the
globals were in another. My friend wrote a pong game in C++ and his was in
server files, I've done mine the exact same way except with the hungarian
notations. His works great! although it was written in MSVC++ 4 not 6

Thanks for the help i'll try that and see if it works.
 
J

John Harrison

Daniel Moree said:
I've got several, I've been programming C/C++ for a while now and i'm just
moving to multiple files. I've got C++ Primer Plus, Teach yourself C in 24
hours, and a few others.

Don't these books explain how to organise your code into seperate source
files?

Haven't rad either but here's a bad review of C++ Primer Plus,
http://www.accu.org/cgi-bin/accu/rvout.cgi?from=0ti_cp&file=cp003131a. Of
course the title of the other is a joke.

I've always had my programs working great. My
program will work fine and I've had it working fine before. Why it doesn't
work now I don't know.

Neither do I.
This is the same setup I've had for a few other
programs in several files although the main code was in one file and the
globals were in another. My friend wrote a pong game in C++ and his was in
server files, I've done mine the exact same way except with the hungarian
notations. His works great! although it was written in MSVC++ 4 not 6

The whole point of seperate cpp files is that you compile the files
seperately. Sure you can make it work if you include one cpp file in another
but then you aren't compiling seperately any more. So what's the point. You
might as well cut and paste the contents of one cpp file into the other, it
amounts to the same thing.
Thanks for the help i'll try that and see if it works.

Here's complete example, two cpp files, main.cpp and other.cpp and a header
file header.h connecting them. It has a global variable and a function which
is called from main.cpp but is defined in other.cpp, so it's pretty similar
to your code. Notice how the header file only contains declarations and the
cpp files contain definitions, and the cpp files include the header. Really
that is all there is to it.

/****** main.cpp *********/
#include "header.h"

// definition of x
int x;

int main()
{
x = 22;
print_x();
}

/****** other.cpp *********/
#include "header.h"
#include <iostream>

// definition of print_x
void print_x()
{
std::cout << "x is " << x << '\n';
}

/****** header.h *********/
#ifndef HEADER_H
#define HEADER_H

// declaration of x
extern int x;

// declaration of print_x
void print_x();

#endif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top