Some issues with .h and .hpp files
Mar 20, 2014 at 9:16pm UTC
Last edited on Mar 21, 2014 at 11:20pm UTC
Mar 21, 2014 at 11:22pm UTC
Do you guys need some more information to help me solve this problem?
Mar 21, 2014 at 11:25pm UTC
Your code could help.
Mar 22, 2014 at 5:23pm UTC
Sorry guys, I thought the code wouldn't matter due the priority of #inlcude
main.cpp:
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
// KI Projekt 2.0 (c) Patrick Höfer
#include <fstream>
#include <iostream>
#include <string>
#include "Controls.h"
#include "Screen_output.h"
using namespace std;
int main()
{
Controls ctrl;
Screen_output draw;
draw.create_field("field.txt" );
draw.screen("title.txt" );
for (;;)
{
int x = ctrl.choose();
if (x == 1)
{
(system("CLS" ));
for (;;)
{
draw.field();
}
}
else
std::cout << "Diese Option ist nicht möglich" ;
}
system("PAUSE" );
return EXIT_SUCCESS;
}
Screen_output.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#ifndef Screen_output_h
#define Screen_output_h
using namespace std;
class Screen_output
{
int field_size;
const char * field_name;
public :
void enemy_spawn(char *);
void create_field(const char *);
void screen(const char *);
void field();
char * file_to_array();
} draw;
#endif
Controls.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
ifndef Controls_h
#define Controls_h
using namespace std;
class Controls
{
public :
int move(char *,int );
int choose();
} ctrl;
#endif
Screen_output.hpp:
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
#include <iostream>
#include <fstream>
#include <string>
#include "Screen_output.h"
void Screen_output::enemy_spawn(char * array)
{
array[800] = 'O' ;
}
void Screen_output::create_field(const char * file_n)
{
field_name = file_n;
}
void Screen_output::screen(const char * file_n)
{
string text_line;
ifstream file (file_n);
if (file.is_open())
{
while (getline(file, text_line))
{
cout << text_line << endl;
}
file.close();
}
}
void Screen_output::field()
{
char * array = file_to_array();
static int func_run = 0;
if ((func_run++) == 0) // Screen Output before first movement
{
array[939] = 'X' ;
for (int i = 0; i < field_size-1; i++)
{
cout << array[i];
}
array[939] = ' ' ; // delete X Spot after first draw occured
}
enemy_spawn(array);
static int pos_offset = 0;
pos_offset += ctrl.move(array, pos_offset);
cout << pos_offset;
array[939+pos_offset] = 'X' ; //create Player Spot X
(system("CLS" )); // clear screen before new draw
for (int i = 0; i < field_size-1; i++)
{
cout << array[i];
}
}
char * Screen_output::file_to_array()
{
field_size = 1799;
char * array = new char [field_size];
int position = 0;
ifstream file (field_name);
if (file.is_open())
{
while (!file.eof())
{
file.get(array[position]);
position++;
}
file.close();
}
return array;
}
Controls.hpp:
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <conio.h>
#include "Controls.h"
int Controls::move(char * array, int pos_offset)
{
char x = getch();
for (;;)
{
if (x == 'w' && array[939+pos_offset-75] != 'X' )
{
if (array[939+pos_offset-75] == 'O' )
{
draw.screen("enemy.txt" );
}
return -75;
}
else if (x == 's' && array[939+pos_offset+75] != 'X' )
{
if (array[939+pos_offset+75] == 'O' )
{
draw.screen("enemy.txt" );
}
return 75;
}
else if (x == 'a' && array[939+pos_offset-1] != 'X' )
{
if (array[939+pos_offset-1] == 'O' )
{
draw.screen("enemy.txt" );
}
return -1;
}
else if (x == 'd' && array[939+pos_offset+1] != 'X' )
{
if (array[939+pos_offset+1] == 'O' )
{
draw.screen("enemy.txt" );
}
return 1;
}
else
return 0;
}
}
int Controls::choose()
{
int x;
string line;
std::cout << std::endl;
std::cout << "Gebe deine Auswahl ein: " ;
getline(cin, line);
stringstream(line) >> x;
return x;
}
Mar 22, 2014 at 9:47pm UTC
In your 'screen_output.hpp' file, you need to #include "Controls.h". Also, are you compiling your HPP files? Because if you are, you will need to declare 'draw' and 'ctrl' as extern to prevent linker errors from occurring.
Mar 23, 2014 at 8:11am UTC
Well I don't really know if I compile them. I use Dev C++ and add these .cpp files to my Project via Dev C++ GUI.
can you anyhow give me a simple but concrete explanation of how #include works?
Because I just added everything as you told me but still a similar error:
http://my.jetscreenshot.com/demo/20140323-wsbg-21kb
I changed the code as followed:
main.cpp:
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
// KI Projekt 2.0 (c) Patrick Höfer
#include <fstream>
#include <iostream>
#include <string>
#include "Controls.h"
#include "Screen_output.h"
using namespace std;
int main()
{
draw.create_field("field.txt" );
draw.screen("title.txt" );
for (;;)
{
int x = ctrl.choose();
if (x == 1)
{
(system("CLS" ));
for (;;)
{
draw.field();
}
}
else
std::cout << "Diese Option ist nicht möglich" ;
}
system("PAUSE" );
return EXIT_SUCCESS;
}
Screen_output.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#ifndef Screen_output_h
#define Screen_output_h
using namespace std;
extern Screen_output draw;
class Screen_output
{
int field_size;
const char * field_name;
public :
void enemy_spawn(char *);
void create_field(const char *);
void screen(const char *);
void field();
char * file_to_array();
} ;
#endif
Controls.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef Controls_h
#define Controls_h
using namespace std;
extern Controls ctrl;
class Controls
{
public :
int move(char *,int );
int choose();
};
#endif
Screen_output.cpp:
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
#include <iostream>
#include <fstream>
#include <string>
#include "Screen_output.h"
#include "Controls.h"
void Screen_output::enemy_spawn(char * array)
{
array[800] = 'O' ;
}
void Screen_output::create_field(const char * file_n)
{
field_name = file_n;
}
void Screen_output::screen(const char * file_n)
{
string text_line;
ifstream file (file_n);
if (file.is_open())
{
while (getline(file, text_line))
{
cout << text_line << endl;
}
file.close();
}
}
void Screen_output::field()
{
char * array = file_to_array();
static int func_run = 0;
if ((func_run++) == 0) // Screen Output before first movement
{
array[939] = 'X' ;
for (int i = 0; i < field_size-1; i++)
{
cout << array[i];
}
array[939] = ' ' ; // delete X Spot after first draw occured
}
enemy_spawn(array);
static int pos_offset = 0;
pos_offset += ctrl.move(array, pos_offset);
cout << pos_offset;
array[939+pos_offset] = 'X' ; //create Player Spot X
(system("CLS" )); // clear screen before new draw
for (int i = 0; i < field_size-1; i++)
{
cout << array[i];
}
}
char * Screen_output::file_to_array()
{
field_size = 1799;
char * array = new char [field_size];
int position = 0;
ifstream file (field_name);
if (file.is_open())
{
while (!file.eof())
{
file.get(array[position]);
position++;
}
file.close();
}
return array;
}
Controls.cpp:
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <conio.h>
#include "Controls.h"
#include "Screen_output.h"
int Controls::move(char * array, int pos_offset)
{
char x = getch();
for (;;)
{
if (x == 'w' && array[939+pos_offset-75] != 'X' )
{
if (array[939+pos_offset-75] == 'O' )
{
draw.screen("enemy.txt" );
}
return -75;
}
else if (x == 's' && array[939+pos_offset+75] != 'X' )
{
if (array[939+pos_offset+75] == 'O' )
{
draw.screen("enemy.txt" );
}
return 75;
}
else if (x == 'a' && array[939+pos_offset-1] != 'X' )
{
if (array[939+pos_offset-1] == 'O' )
{
draw.screen("enemy.txt" );
}
return -1;
}
else if (x == 'd' && array[939+pos_offset+1] != 'X' )
{
if (array[939+pos_offset+1] == 'O' )
{
draw.screen("enemy.txt" );
}
return 1;
}
else
return 0;
}
}
int Controls::choose()
{
int x;
string line;
std::cout << std::endl;
std::cout << "Gebe deine Auswahl ein: " ;
getline(cin, line);
stringstream(line) >> x;
return x;
}
Mar 23, 2014 at 9:23am UTC
OK, two things. First, you need to move your extern declarations of the variables to
after you define the class. Once you have done that, you need to declare them inside a source file as well. Here is an example of what it should look like now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Screen_Output.h
#ifndef Screen_output_h
#define Screen_output_h
// using namespace std; - Don't have this in header files. You didn't need it anyway.
class Screen_output {
// ...
};
extern Screen_output draw;
#endif
1 2 3 4 5 6 7 8 9 10 11
// Controls.h
#ifndef Control_h
#define Control_h
class Controls {
// ...
};
extern Controls ctrl;
#endif
1 2 3 4 5 6 7
// Controls.cpp
#include ... // your include files
Controls ctrl;
//...
1 2 3 4 5 6 7
// Screen.cpp
#include ... // your include files
Sceen_output draw;
// ...
And then main as you have it.
Last edited on Mar 23, 2014 at 9:23am UTC
Topic archived. No new replies allowed.