header file construction

So i have this code in my header file:

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

#include <vector>
#include <map>
#include "PuzzleRules.cpp"
template<class configType>
class Solver
{
public:
	Solver(PuzzleRules& rules); //line 26
	~Solver(){};
	std::vector<configType> evaluate(vector<configType> initialData);
private:
	PuzzleRules* puzzleRules;    // line 30
};
#endif //SOLVER_H_


to which the compiler returns:


Solver.h:26: error: expected `)' before '&' token
Solver.h:30: error: ISO C++ forbids declaration of `PuzzleRules' with no type
Solver.h:30: error: expected `;' before '*' token


I think i might be able to get around the first problem by just passing the solver a reference at construction and removing '&' in the header file prototype. The second problem on line 30 i am less sure of and I'm hoping someone can point me in the right direction.
Don't include cpp files.

line 6 probably should be #include "PuzzleRules.h"


EDIT: firedraco has been NINJA'd
Last edited on
Why are you including a .cpp file?

Also, it appears to be that you haven't defined PuzzleRules anywhere. I think you might have meant to include the .h(pp) file instead?
Yes, include .hpp files instead of .cpp!

#include "PuzzleRules.hpp"

I find that this system of including files works wonders;

main.hpp, yes i know it's the header for main, but this way all your cpp files include it (main.cpp for example), and this includes all your headers. You'll never have linking errors again, however you may need to order your include for dependency issues.

example from one of my games for the console showing my preferred including method;

main.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
84
85
86
87
88
89
90
#ifndef MAIN_HPP_INCLUDED
#define MAIN_HPP_INCLUDED
///Included STL's
#include <iostream>
#include <vector>
#include <cstdlib>
#include <fstream>
#include <time.h>
#include <sstream>
#include <stdlib.h>
///Include Other Libraries
///Namespaces
using namespace std;
///Custom Defines
#define null 0
#define line(x,id) for(int line_loop=0;line_loop<x;line_loop++){cout<<id;};cout<<"\n";
#define this_macro_sucks(x,y) ;
#define loop end=false;while(!end)
#define transform(var,into,newtype) dynamic_cast<newtype>(var)
#define ldh count++;if (!suppress)
#define cierto true
#define KTHXBYE ;
#define SEPARATE ;
#define STUPID_FURRY for(int i=0;i<group.size();i++)
#define SEMI_COLON(x) x;
#define gs s="";getline(myfile,s,'-');
#define gss(var) gs ; var=s;
#define gsi(var) gs ; var=atoi(s.c_str());
#define MAX(var) if(var>cap)var=cap;
#define MIN(var) if(var<0)var=0;
///Class Prototypes
//Unit and inherited classes
class Unit;

class Player;

class Monster;
class MONSTER;//Basic class to copy and paste for new monsters.

class Orc;
class Hydra;
class Wizard;
class Dragon;
class Priest;
class Rock_Golem;
class Spider;
class Snake;
class Ghost;
//Other Stuff
class Item;
class Stats;
class Party;
class DungeonWalker;
//class Battle;
///Global Functions
string to_s(int i);
int Battle(Party* Team1,Party* Team2);
void Ssleep(unsigned int mseconds);
float RndFloat(float Begin,float End);
int RI(int Begin,int End);
void FocusPoint(Stats* s,int num);
Stats CreateChar(int playernum);
int main();
///Include System Headers
#include "Stats.hpp"

#include "Party.hpp"

#include "Unit.hpp"
#include "Player.hpp"

#include "Item.hpp"

#include "Room.hpp"
#include "DungeonWalker.hpp"
///Include Monster Headers
#include "Monster.hpp"
#include "Monsters/Blank_Monster.hpp"
#include "Monsters/Orc.hpp"
#include "Monsters/Hydra.hpp"
#include "Monsters/Wizard.hpp"
#include "Monsters/Dragon.hpp"
#include "Monsters/Priest.hpp"
#include "Monsters/Necromancer.hpp"
#include "Monsters/Rock Golem.hpp"
#include "Monsters/Ghost.hpp"
#include "Monsters/Spider.hpp"
#include "Monsters/Snake.hpp"
///Anything Else
#endif // MAIN_HPP_INCLUDED 

main.cpp
1
2
#include "main.hpp"
...
@theexactpoint: You probably don't need the include at all. Just forward declare (class PuzzleRules; at the beginning)

Ignatus wrote:
however you may need to order your include for dependency issues.
That should not happen (it even bothers have to maintain the linking order, like in SFML). Please read this http://www.cplusplus.com/forum/articles/10627/#msg49680
However I remember something about having to include cstdlib before GLUT (probably a bug)

but this way all your cpp files include it
So you end including unnecessaries* files, you will be recompiling when you shouldn't and the process of the files takes time.

Also don't using on headers (especially before includes) it defeats the purpose of having namespace

*how it should be written?
Last edited on
Topic archived. No new replies allowed.