include & precompiled headers

I have such problem. I try to include header of class into StdAfx.h

projectfolder/model/ChangePlayerVisitor.cpp:
1
2
3
4
5
6
7
#include "StdAfx.h" // #include "ChangePlayerVisitor.h"

ChangePlayerVisitor::ChangePlayerVisitor(int to)
: _player(to)
{
}
// code continues 


projectfolder/model/StdAfx.h
1
2
3
#include "ChangePlayerVisitor.h"
#include "Condition.h"
#include "TriggerVisitor.h"  


Error:
\model\changeplayervisitor.cpp(3): error C2653: 'ChangePlayerVisitor' : is not a class or namespace name


The code for projectfolder/model/ChangePlayerVisitor.h

1
2
3
4
5
6
7
8
9
10
11
12
13

#include "TriggerVisitor.h"

class ChangePlayerVisitor : public TriggerVisitor // It tells the compiler that the class ChangePlayerVisitor 'inherits' publicly from the existing class TriggerVisitor. 
{
public:
	ChangePlayerVisitor(int to);
	void visit(Trigger&);
	void visit(Effect&);
	void visit(Condition&);
private:
	int _player;
};
Last edited on
closed account (Dy7SLyTq)
can you show us stdafx?
Stdafx.h

1
2
3
#include "TriggerVisitor.h"
#include "ChangePlayerVisitor.h"
#include "Condition.h" 
Last edited on
closed account (Dy7SLyTq)
sorry i dont know what i was thinking... its not seeing your class ChangePlayerVisitor. i would just explicitlly include it in ChangePlayerVisitor.cpp
So it must be there? I wanted to include it do stdafx to make the program compile faster because it is robust.. But why does it not see it?

Change made
#include "TriggerVisitor.h"
#include "ChangePlayerVisitor.h"
#include "Condition.h"

TriggerVisitor.h needs to be loaded before ChangePlayerVisitor.h

ChangePlayerVisitor is descendant of TriggerVisitor

Should not TriggerVisitor.cpp be compiled before ChangePlayerVisitor.cpp ?
Last edited on
Do I need StdAfx.cpp? Because if I create it and place:
#include "./model/StdAfx.h"
so it prints
visual studio 10.0\vc\include\xlocnum(133): error C2857: '#include' statement specified with the /YcStdAfx.h command-line option was not found in the source file

**************************

If I include
#include "./Model/TriggerVisitor.h"
in stdafx.h included from stdafx.cpp (first file included)
so I got error of redefinition
error C2011: 'TriggerVisitor' : 'class' type redefinition

but If I do not include
#include "TriggerVisitor.h"
in TrigXMLVisitor.h So I have plenty of errors missing this file I got error
\model\changeplayervisitor.cpp(3): error C2653: 'ChangePlayerVisitor' : is not a class or namespace name
Last edited on
> TriggerVisitor.h needs to be loaded before ChangePlayerVisitor.h
That shouldn't happen. Each header should be self contained, and the order of inclusion should make no difference.
You don't seem to be using headers guards, read http://www.cplusplus.com/forum/articles/10627/

> Should not TriggerVisitor.cpp be compiled before ChangePlayerVisitor.cpp
That would depend on your makefile, but it's irrelevant.


You could stop the process at the preprocessor stage, so you can see how the files are loaded
(include is equivalent to copy paste)
-E flag for gcc
/P for vs http://msdn.microsoft.com/en-us/library/8z9z0bx6(v=vs.90).aspx
OK, thanks. I did the changes to include guard.
Last edited on
Anyway I have yet question.

Even that I use stdafx with precompiled headers it does not same my time. It compiles every time when I did change in cpp code.

Example:
Header:
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include <map>
#include <string>
#include <iostream>
#include <functional>
#include <conio.h> 


cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdafx.h"
using namespace std;

int main()
{
    map<string,string,greater<string> > myArray;
    myArray.insert(make_pair(string("Key0"),string("val4 blabla")));
    myArray.insert(make_pair(string("Key1"),string("val1")));
    myArray.insert(make_pair(string("Key2"),string("val2 blabla")));
    cout << myArray[string("Key1")] << endl;
	_getch();
    return 0;
}


I always press alt+ctrl+f7 (rebuild) and it rebuilds even the stdafx!

Only with f5 (debug) it saves my time.
Last edited on
> I always press alt+ctrl+f7 (rebuild) and it rebuilds even the stdafx!
http://stackoverflow.com/questions/3095901/difference-between-build-solution-rebuild-solution-and-clean-solution-in-visua
Try just `build'
Should I first use the Precompiled headers options "Create" headers and then "Use" headers? I seems to me that it has not effect.

So I wait about 10 minutes till all the files are comiled, and then the last file resources.h which I inserted into stdafs.h prints error unexpected end of file. Is it because I inserted header guard into the file or should I not add it into stdafx?
unexpected end of file found:
#endif // last line of H_resource


Also is there some way how to make fast check if all files which I try to include in the project have correct path or file name? It is silly to wait 10 minutes and then to read "file xyz.h does not exist"...
Last edited on
Compile only the unit that you are interested in, or just run the preprocessor for all.

> the last file resources.h which I inserted into stdafs.h prints error unexpected end of file
¿do you really expect me to diagnostic that with so little information?
The file didn't end well, it stopped at the middle of a

> Is it because I inserted header guard into the file or should I not add it into stdafx?
It is not a good idea to put includes in stdafx.h of files that would be changing a lot.
It defeats its purpose.
I still have problems with it. I have already compiled it successfully with Create (/Yc) option. It took cca 20 seconds. I don't know how could I compile only one unit (file cpp) do you mean key F7?.

But I cannot compile with Use (/Yu) options. It prints error that there are no pch files in Debug folder! However when I run it on Create options so no problem with it, only very short time waiting per one cpp file.

Here are all the errors:
http://paste.ofcode.org/qhKXtK7Z9DqNJpAvz2UJxC

Topic archived. No new replies allowed.