Reputation: 19
Compiler show me 2 errors:
C2146 - syntax error: missing ';' before identifier 'begin'
C4430 - missing type specifier - int assumed. Note: C++ does not support default-int
Errors occur in my "Globals.h"
file, at line 5.
It's here:
#pragma once
#include "Load.h"
#include "SortStream.h"
extern Load begin;
extern int sort_by;
extern int sort_type;
extern vector<Flight> all_flights;
extern SortStream sort_stream;
My "Globals.cpp"
file looks like:
#include "Globals.h"
Load Begin;
int sort_by;
int sort_type;
vector<Flight> all_flights;
SortStream sort_stream;
And ofc, here is "Load.h"
file:
#pragma once
#include <vector>
#include "Flight.h"
#include "Globals.h"
using namespace std;
class Load {
std::string path;
vector<Flight> allFlights;
public:
Load();
std::string get_path();
vector<Flight> get_allFlights();
void set_path(std::string p);
void set_allFlights(vector<Flight> v);
void load_flights();
};
And "SortStream.h"
file:
#pragma once
#include <vector>
#include "Flight.h"
using namespace std;
class SortStream {
vector< vector < Flight > > layout;
int switchNum;
int iterationNum;
int compareNum;
public:
SortStream();
vector< vector < Flight > > get_layout();
int get_switchNum();
int get_iterationNum();
int get_compareNum();
void set_layout(vector< vector < Flight > > v);
void set_switchNum(int n);
void set_iterationNum(int n);
void set_compareNum(int n);
};
Does anyone knows reason? Tnx in advance
Upvotes: 1
Views: 426
Reputation: 33931
To see the fail, you have to understand how #includes
work. Each #include
is replaced by a copy of the included file.
Looking at Load.h
#pragma once
#include <vector> <-- paste vector in here
#include "Flight.h" <-- paste Flight.h in here
#include "Globals.h" <-- paste Globals.h in here
using namespace std;
class Load {
std::string path;
vector<Flight> allFlights;
public:
Load();
std::string get_path();
vector<Flight> get_allFlights();
void set_path(std::string p);
void set_allFlights(vector<Flight> v);
void load_flights();
};
Let's paste in Globals.h and watch what happens:
#pragma once
#include <vector> <-- paste vector in here
#include "Flight.h" <-- paste Flight.h in here
//Globals.h begins here
#pragma once
#include "Load.h" <-- would paste Load.h in here, but it's protected by #pragma once
and will not be pasted in again
#include "SortStream.h" <-- paste SortStream.h in here
extern Load begin;
extern int sort_by;
extern int sort_type;
extern vector<Flight> all_flights;
extern SortStream sort_stream;
// end of Globals.h
using namespace std; <-- off topic: Very dangerous thing to do in a header
class Load {
std::string path;
vector<Flight> allFlights;
public:
Load();
std::string get_path();
vector<Flight> get_allFlights();
void set_path(std::string p);
void set_allFlights(vector<Flight> v);
void load_flights();
};
In this case we can see that Load begin;
is referenced before Load
is defined. Boom.
Circular references are almost always bad and in this case, it is lethal. In other words, Tarik Neaj for the win.
Load.h has no need for anything defined in Globals.h, so remove the include to break the cycle.
Upvotes: 1