Weird errors when using .cpp file

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:

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
#ifndef FJSP_JOBPATTERN
#define FJSP_JOBPATTERN

#include <vector>
#include <map>
#include <sstream>

#include "Job.h"
#include "MachineType.h"
#include "Operation.h"

using namespace std;

class JobPattern {
private:
	struct Key {
		MachineType *type;
		int time;
		Key(MachineType *type, int time) : type(type), time(time) { }
		bool operator==(const Key &k) const {
			return this->type == k.type && time == k.time;
		}
		bool operator<(const Key &k) const {
			return this->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 ==========


By the way, I am using Visual Studio 2010.

Any help is greatly appreciated :)
Last edited on
The first error is in Operation.h. Try to fix that error first.
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...
If there is nothing wrong with Operation.h then it must be the code you have above the include that cause the error in Operation.h.
1
2
3
4
5
6
7
8
9
#ifndef FJSP_JOBPATTERN
#define FJSP_JOBPATTERN

#include <vector>
#include <map>
#include <sstream>

#include "Job.h"
#include "MachineType.h" 


Make sure that you didn't forget to put a semicolon after the class definitions in Job.h and MachineType.h.

Note that the whole program will compile and run fine if you have a header file with errors that is never included.
Last edited on
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...
Can you show some more code? At least the first 8 rows of Operation.h.
Sure, these are the complete contents of Operation.h:

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

class MachineType;

class Operation {
public:
	string name;
	MachineType *target;
	int processingTime;
	int idleTimeAfter;

	Operation(string name, MachineType *target, int processingTime, int idleTimeAfter):
		name(name), target(target), processingTime(processingTime), idleTimeAfter(idleTimeAfter) { }
};

#endif 
in Operation.h an #include <string> is missing.

oh and since not using namespace std; you need to write std::string
Last edited on
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 using namespace std; before including Operation.h and that's why there was no error.
Ok, thanks a lot! :)
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.

HTH
Topic archived. No new replies allowed.