Including functions and variables from a separate header and cpp file.

Hello

I'm a bit stuck and was wondering if anyone is willing to help.

What im trying to do is at the top of my main.cpp function is to include a file which contains all the global variables and functions in.

Unfortunately ive also got a separate class file which requires some of these global variables.

Im getting the following error:

1
2
3
1>c:\users\luke\desktop\pong\main.cpp(36): error C3861: 'init': identifier not found
1>c:\users\luke\desktop\pong\main.cpp(42): error C3861: 'load_files': identifier not found
1>c:\users\luke\desktop\pong\main.cpp(58): error C2065: 'event' : undeclared identifier


And this is part of my header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef	HEADER1_H
#define HEADER1_H

#include <iostream>

using namespace std;

SDL_Surface *load_image( std::string filename );

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip );

bool init();

bool load_files();

void clean_up();

#endif 


Which has the functions in the correct cpp file.

Ive included this header file into my class file and main.cpp file

Any help would be appreciated, thanks
Nothing is wrong with that header file (although you don't need/shouldn't have that "using namespace std" in there).

Show us your main.cpp file.

Here is my main.cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//The headers
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "Dot.h"
#include "Timer.h"
#include "header.h"
#include <sstream>
#include <string>

int main( int argc, char* args[] )
{


    //Quit flag
    bool quit = false;
	
	int frame = 0;
	bool cap = true;

    //The dot that will be used
    Dot myDot;

    //The frame rate regulator
    Timer fps;
	Timer update;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //Load the files
    if( load_files() == false )
    {
        return 1;
    }

	update.start();

	//Start the frame timer
       fps.start();

    //While the user hasn't quit
    while( quit == false )
    {


        //While there's events to handle
        while( SDL_PollEvent( &event ) )
        {
			                //If enter was pressed
                if( event.key.keysym.sym == SDLK_RETURN )
                {
                    //Switch cap
                    cap = ( !cap );
                }
            //Handle events for the dot
            myDot.handle_input();

            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }

        //Move the dot
        myDot.move();

		apply_surface(0,0, background, screen);

		apply_surface( 0,0, sprite, screen );
		
        //Show the dot on the screen
  		myDot.show();

		        if( SDL_Flip( screen ) == -1 )
        {
            return 1;
        }

		frame++;
        //Update the screen

		 if( update.get_ticks() > 1000 )
        {
            //The frame rate as a string
            std::stringstream caption;

            //Calculate the frames per second and create the string
            caption << "Average Frames Per Second: " << frame / ( fps.get_ticks() / 1000.f );

            //Reset the caption
            SDL_WM_SetCaption( caption.str().c_str(), NULL );

            //Restart the update timer
            update.start();
        }


		        //Cap the frame rate
        if( (cap == true) && (fps.get_ticks() < 1000 / FRAMES_PER_SECOND ))
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
    }

    //Clean up
    clean_up();

    return 0;
}


Thanks for the help.
is your file name header.h or header1.h?

main is including header.h but your first post suggests the name is header1.h
Yea they are named correctly.

Erm they were named correctly, but something went wrong as it said that I already had the header.h file.
So i started a new project and just pasted the code over.

Now the problem is, where should I put my variables that the header file require? Its not a class so I cant put them into a constructor.

1
2
3
4
5
1>c:\users\luke\documents\visual studio 2010\projects\sdl test\sdl test\main.cpp(74): error C2065: 'background' : undeclared identifier
1>c:\users\luke\documents\visual studio 2010\projects\sdl test\sdl test\main.cpp(74): error C2065: 'screen' : undeclared identifier
1>c:\users\luke\documents\visual studio 2010\projects\sdl test\sdl test\main.cpp(75): error C2065: 'sprite' : undeclared identifier
1>c:\users\luke\documents\visual studio 2010\projects\sdl test\sdl test\main.cpp(75): error C2065: 'screen' : undeclared identifier
1>c:\users\luke\documents\visual studio 2010\projects\sdl test\sdl test\main.cpp(80): error C2065: 'screen' : undeclared identifier


Also what should I do with my event? SDL_Event event;

Am I approaching this all wrong? The code was working when it was all in the main.cpp file, but I wanted to make things more organized and it seems to off just broken things lol.

Thanks for the help again :)
In your CPP file and H files, do a CTRL+F for "background", "screen" and "sprite". You'll see that you haven't defined it anywhere. These need to be of a specific type.

Also, I don't see SDL_Event anywhere in your code. I assume it must be in SDL.h or something, but you don't have SDL_Event event available to your CPP file. I'm surprised you don't have a compiler error for that too. Make sure that you have that somewhere.
Note: It's possible that you have these objects defined in another header file, however unless you use the extern keyword in the header and then re-define it in a cpp file somewhere, it will either not be available to this file, or it will create a compiliation error when you try to include that header twice.
Topic archived. No new replies allowed.