I have created a header file for an application I am writing but now that it receives more functionality than intended at first, I would like to move the member function bodies to a .cpp file. I have done this with tons of other classes and it always worked fine, however now I'm receiving the weirdest errors. If I leave all the functionality in the header file, everything compiles and runs perfectly. However, when I just create a .cpp file and put an #include statement there to include the header file, I get these weird errors and these errors vanish when I comment out the #include statement, such that an empty file remains. I was wondering if somebody has any clue about what might be causing this.
This is my current header with the name JobPattern.h:
#ifndef FJSP_JOBPATTERN
#define FJSP_JOBPATTERN
#include <vector>
#include <map>
#include <sstream>
#include "Job.h"
#include "MachineType.h"
#include "Operation.h"
usingnamespace std;
class JobPattern {
private:
struct Key {
MachineType *type;
int time;
Key(MachineType *type, int time) : type(type), time(time) { }
booloperator==(const Key &k) const {
returnthis->type == k.type && time == k.time;
}
booloperator<(const Key &k) const {
returnthis->type->name < k.type->name || time < k.time;
}
};
map<Operation *, int> startingTimes;
map<Key, int> usedMachines;
public:
Job *job;
JobPattern(Job *job, vector<MachineType> *machineTypes, int timeHorizon) : job(job) {
//initialize the usedMachines map by setting everything to 0
for(vector<MachineType>::iterator it=machineTypes->begin(); it!=machineTypes->end(); it++) {
for(int t=0; t<timeHorizon; t++) {
usedMachines[Key(&(*it), t)] = 0;
}
}
}
void setStartingTime(Operation *o, int startingTime) {
this->startingTimes[o] = startingTime;
}
int getStartingTime(Operation *o) {
map<Operation *, int>::iterator it = this->startingTimes.find(o);
if(it == this->startingTimes.end()) {
ostringstream oss;
oss << "StartingTime not found for operation " << o->name;
throw exception(oss.str().c_str());
}
return it->second;
}
int getUsedMachines(MachineType *type, int time) {
map<Key, int>::iterator it = this->usedMachines.find(Key(type, time));
if(it == this->usedMachines.end()) {
ostringstream oss;
oss << "UsedMachines not found for machinetype " << type->name << " and time " << time;
throw exception(oss.str().c_str());
}
return it->second;
}
};
#endif
And the only thing I put in JobPattern.cpp is:
#include "JobPattern.h"
And then, the weird errors:
1>------ Build started: Project: FJSP, Configuration: Debug x64 ------
1> JobPattern.cpp
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Operation.h(8): error C2146: syntax error : missing ';' before identifier 'name'
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Operation.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Operation.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Operation.h(13): error C2061: syntax error : identifier 'string'
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Operation.h(14): error C2065: 'name' : undeclared identifier
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Operation.h(14): error C2614: 'Operation' : illegal member initialization: 'name' is not a base or member
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Job.h(10): error C2146: syntax error : missing ';' before identifier 'name'
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Job.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Job.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Job.h(11): error C2143: syntax error : missing ';' before '<'
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Job.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Job.h(11): error C2039: 'iterator' : is not a member of '`global namespace''
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Job.h(11): error C2238: unexpected token(s) preceding ';'
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Job.h(14): error C2061: syntax error : identifier 'string'
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Job.h(15): error C2065: 'name' : undeclared identifier
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Job.h(15): error C2065: 'firstOperation' : undeclared identifier
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Job.h(15): error C2614: 'Job' : illegal member initialization: 'firstOperation' is not a base or member
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Job.h(15): error C2614: 'Job' : illegal member initialization: 'name' is not a base or member
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\MachineType.h(6): error C2146: syntax error : missing ';' before identifier 'name'
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\MachineType.h(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\MachineType.h(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\MachineType.h(9): error C2061: syntax error : identifier 'string'
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\MachineType.h(9): error C2065: 'name' : undeclared identifier
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\MachineType.h(9): error C2614: 'MachineType' : illegal member initialization: 'name' is not a base or member
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\JobPattern.h(24): error C2039: 'name' : is not a member of 'MachineType'
1> c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\MachineType.h(4) : see declaration of 'MachineType'
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\JobPattern.h(24): error C2039: 'name' : is not a member of 'MachineType'
1> c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\MachineType.h(4) : see declaration of 'MachineType'
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\JobPattern.h(48): error C2039: 'name' : is not a member of 'Operation'
1> c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\Operation.h(6) : see declaration of 'Operation'
1>c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\JobPattern.h(57): error C2039: 'name' : is not a member of 'MachineType'
1> c:\users\claire\desktop\dropboxbert\dropbox\uu\master\research project\code\fjsp\MachineType.h(4) : see declaration of 'MachineType'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
There are no errors in other files, the whole application compiles and runs perfectly when I don't use JobPattern.cpp and leave all functionality in the header file...
That's the weird thing, I have double checked everything for semicolons and all possible causes that I can come up with and have dealt with a load of times. I'm sure that all other code works fine, since I'm definitely using the code from the header "JobPattern.h" in other classes right now. I include the header and create instances of the JobPattern class and everything works fine... I also tried to clean the solution and rebuild everything, but unfortunately nothing seems to work...
Damned, you are absolutely right! I had some header files that didn't include <string> and hadn't the line "using namespace std;". What bothers me is how it is possible that the compiler didn't complain about this earlier. Does anyone have a clue about why the compiler is perfectly fine with leaving out these includes in the first case and then starts complaining when I add a .cpp file...?
All that #include does is that it works like a copy paste of the file content into the file. Instead of writing #include "Operation.h" you could have copy pasted the content of Operation.h and the result would have been exactly the same.
In the other files you probably had #include <string> and usingnamespace std; before including Operation.h and that's why there was no error.
I am wondering why you have implementation code a header file.
Normally, declarations of classes with their member variables and functions, go in the header file. The .cpp file has the implementation of the functions.
I am sure you can get to compile, I just wanted to mention what I thought was the normal situation.
On my IDE (KDevelop or QtCreator), when I put a function in the header file, I can get it to copy that function to the implementation file (.cpp) automatically. I am guessing you can do the same on your IDE.
I thought the reason for this was, so you could include the header file into any .cpp file that needs it. You don't want it to execute code in that situation.