I am using Visual Studio 2005. I created an MFC based console application named "StdAfx dependancy". The IDE created the following files for me.
Resource.h
StdAfx Dependancy.h
stdafx.h
StdAfx Dependancy.cpp
stdafx.cpp
I added another class CHelper with Helper.h and Helper.cpp as below.
Helper.h:
#pragma once
class CHelper
{
public:
CHelper(void);
~CHelper(void);
};
Helper.cpp
#include "StdAfx.h"
#include "Helper.h"
CHelper::CHelper(void)
{
}
CHelper::~CHelper(void)
{
}
I created an object for CHelper in the main function; to achieve this I added Header.h file in the first line of StdAfx Dependancy.cpp as below; and I got the following errors.
d:\codes\stdafx dependancy\stdafx
dependancy\stdafx dependancy.cpp(33) :
error C2065: 'CHelper' : undeclared
identifier
d:\codes\stdafx
dependancy\stdafx dependancy\stdafx
dependancy.cpp(33) : error C2146:
syntax error : missing ';' before
identifier 'myHelper'
d:\codes\stdafx
dependancy\stdafx dependancy\stdafx
dependancy.cpp(33) : error C2065:
'myHelper' : undeclared identifier
But when I include it after stdafx.h, the error vanishes. Why?
// Stdafx dependancy.cpp : Defines the entry point for the console application.
//
#include "Helper.h"
#include "stdafx.h"
#include "Stdafx dependancy.h"
// #include "Helper.h" --> If I include it here, there is no compilation error
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
CHelper myHelper;
}
return nRetCode;
}
This link must give you some clue.
Purpose of stdafx.h
The lines defined before the
#include "stdafx.h" are ignored by the compiler. So if you want to actually include those files then you need to include them after the #include "stdafx.h".
Hope it is clear.
In addition to ckv's answer, it makes little sense to include header files before "stdafx.h" as any non-trivial header file will contain framework types that are included there (e.g. any MFC types).
Related
I am trying to run a project using Visual studio 2003. But I am getting lot of compilation errors similar to the following.
The errors are pointing to WinSock2.h file. I am copying couple of code snippets from WinSock2.h file and the corresponding errors
typedef struct fd_set {
u_int fd_count; /* how many are SET? */
SOCKET fd_array[FD_SETSIZE]; /* an array of SOCKETs */
} fd_set;
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\PlatformSDK\Include\WinSock2.h(114): error C2065: 'fd_set' :
undeclared identifier
struct sockaddr {
u_short sa_family; /* address family */
char sa_data[14]; /* up to 14 bytes of direct address */
};
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\PlatformSDK\Include\WinSock2.h(109): error C2143: syntax
error : missing ';' before '{'
The ws2_32.lib file is added to "Configuration properties - Linker - Input - Additional Dependencies". The build configuration platform is win32.
Thanks in advance for your help.
a typical basic Winsock Application with the good order of header files can be found here:
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
int main() {
return 0;
}
The order of including header files is important
while working on a personal project on a new language, I 've come across a annoying compiling time error where a static member of my constants.h (constants class) doesn't find the class name (Quark which is a derived class of Particle, those two in the same file "Particle.h") used in parameter...
Here is the compilation error output:
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(41): error C2065: 'Quark' : undeclared identifier
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(41): error C2923: 'std::vector' : 'Quark' is not a valid template type argument for parameter '_Ty'
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(45): error C2065: 'Quark' : undeclared identifier
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(45): error C2923: 'std::vector' : 'Quark' is not a valid template type argument for parameter '_Ty'
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(45): error C3861: 'Quark': identifier not found
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(45): error C2514: 'std::vector' : class has no constructors
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(46): error C2663: 'std::vector<_Ty,_Alloc>::vector' : 8 overloads have no legal conversion for 'this' pointer
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(50): error C2065: 'Quark' : undeclared identifier
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(50): error C2923: 'std::vector' : 'Quark' is not a valid template type argument for parameter '_Ty'
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(50): error C3861: 'Quark': identifier not found
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(50): error C2514: 'std::vector' : class has no constructors
1>f:\bibliotheques\documents\visual studio 2013\projects\particlefuzzer\launcher\constants.h(51): error C2663: 'std::vector<_Ty,_Alloc>::vector' : 8 overloads have no legal conversion for 'this' pointer
Here the crashing code:
constants.h
#pragma once
#include "stdafx.h"
#include <vector>
#include "Particle.h"
class constants
{
public:
static enum nucl
{
proton = 0
,neutron = 1
};
static std::vector<Quark> getNuclVal(nucl type)
{
if (type == nucl::proton)
{
std::vector<Quark> result({ Quark(std::string("up")), Quark(std::string("up")), Quark(std::string("down")) });
return result;
}
else
{
std::vector<Quark> result({ Quark(std::string("up")), Quark(std::string("down")), Quark(std::string("down")) });
return result;
}
};
// More functions and constants
};
Particle.h
#pragma once
#include <vector>
#include "Entity.h"
#include "constants.h"
class Particle : public Entity
{
private:
// some stuff
public:
// some stuff
};
class Quark : public Particle
{
public:
Quark(std::string &const);
};
and there is my Quark class definition in Particle.cpp
#include "stdafx.h"
#include "Particle.h"
#include "constants.h"
#include <string>
#include "Entity.h"
Particle::Particle(std::string &const name, Size size)
: Entity::Entity()
, m_charge(0) //en e
, m_mass(0) //en electron Volt (eV)/c²
, m_Size(size)
, m_name(name)
{
};
Quark::Quark(std::string &const name) // declaration stuff
: Particle(name, Size::Sub_Atomic)
{
constants::quark type = constants::getQuarkByName(m_name);
setMass(constants::getMass(type));
setCharge(constants::getCharge(type));
}
// various stuffs
There is my precompiled header which include every sources I need in my program (to let you see compilation order) stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <string>
// TODO: reference additional headers your program requires here
#include "Math.h"
#include "SciNumber.h"
#include "PhyEngine.h"
#include "Options.h"
#include "ConfigFile.h"
#include "Chameleon.h"
#include "Entity.h"
#include "Particle.h"
#include "constants.h"
I wonder if I need to separate my derived classes of Particle to specific files or not (It would add a lot of files..... T-T), what do you think about it?
here is my class diagram (open with Visual studio) to help you visualize the whole project: download it here via my dropbox
You have a circular dependency in your headers. Particle.h includes constants.h and constants.h includes Particle.h. You'll need break the loop.
You get the error because when Particle.h is included, it'll at first include constants.h. Then constants.h will try to include Particle.h. #pragma once will prevent infinite recursion, but also means that rest of the Particle.h is not yet included before the contents of the constants.h. Thus the compiler complains about missing declarations.
You should be able to break the loop by breaking your headers and possibly classes into smaller ones*. The static member functions of constants are not strictly related to each other, are they? In fact, from what I've seen, it should probably be a namespace rather than a class**. Stuff in namespace can be conviniently declared in separate headers while a class must be defined entirely in one.
*Assuming you actually need to. If Particle.h doesn't depend on constants.h, then simply remove the include.
**I may be wrong about that since you didn't show the entire class.
Break off the part of constants that depends on Quark (and anything that depends on anything that depends on Quark, recursively) into another class in another header and don't include that one inside Particle.h. Or break off parts of Particle.h that depend on constants.h and don't include that one in constants.h.
stdafx.h also includes constants.h which in turn includes stdafx.h. You'll need to fix all circular includes.
Try moving
#include "constants.h"
to the bottom of Particle.h. What currently happens is that Particle.h goes through constants.h before it has declared the types.
constants.h is not re-including particle.h because of pragma #once (without which you would have circular dependencies and nothing would work anyway).
You also don't need the #include "constants.h" in stdafx.h as it is already included in Particle.h
I hope this helps!
My code is as follows
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <windows.h>
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
BYTE* pAlloc1 = NULL;
return 0;
}
creating following errors.
error C2065: 'BYTE' : undeclared identifier
What am I doing wrong here?
You have #include "stdafx.h", which usually means that you're using a precompiled header. If you use a precompiled header, anything preceding the precompiled header will be discarded.
Try reordering your #include lines so that "stdafx.h" is first. (Or change stdafx.h to #include <windows.h>, which is generally where you want to put commonly-used system headers.)
When I compile my project I get this error.
C:\src\libs\nvrtpaudio\FileRtpSource.
cpp(61) : error C3861: 'timeBeginPeriod': identifier not found
C:\src\libs\nvrtpaudio\FileRtpSource.
cpp(71) : error C3861: 'timeEndPeriod': identifier not found
gmake[5]: *** [_out/win7_x86_debug/FileRtpSource.obj] Error 2
I included windows.h but this error still persists. Anyone know how to resolve this?
MSDN says:
Header: Mmsystem.h (include Windows.h)
So you are expected to include "windows.h" and be fine, but what MSDN does not say is that this assumes you don't have WIN32_LEAN_AND_MEAN defined, which - when defined, and this also can be the case with project created from template - exlcudes "mmsystem.h" you need.
So you have to either make sure you don't have WIN32_LEAN_AND_MEAN in your project, or otherwise include directly:
#include "stdafx.h"
#include <mmsystem.h> // <<--- Here we go
#pragma comment(lib, "winmm.lib")
int _tmain(int argc, _TCHAR* argv[])
{
timeBeginPeriod(0);
return 0;
}
I don't know if I'm going crazy, or just that everything I've read on this error doesn't apply to my situation. But I'm getting these errors when I compile my project:
1>f:\program files\testengine\testengine\testengine\game.cpp(10) : error C2061: syntax error : identifier '{ctor}'
1>f:\program files\testengine\testengine\testengine\game.cpp(11) : error C2143: syntax error : missing ';' before '{'
1>f:\program files\testengine\testengine\testengine\game.cpp(11) : error C2447: '{' : missing function header (old-style formal list?)
1>f:\program files\testengine\testengine\testengine\game.cpp(15) : error C2059: syntax error : 'public'
1>f:\program files\testengine\testengine\testengine\game.cpp(16) : error C2143: syntax error : missing ';' before '{'
1>f:\program files\testengine\testengine\testengine\game.cpp(16) : error C2447: '{' : missing function header (old-style formal list?)
So, I Google'd the error, and everyone said this is caused by things like extra and/or missing semicolons and brackets. But I've looked over my code a lot (there's not very much!) and I don't see any of that, unless of course, as I previously suggested, I'm going crazy...
Game.h
#ifndef _SBE_CGAME_
#define _SBE_CGAME_
class CGame
{
public:
CGame();
~CGame();
void DoLoop();
};
#endif //_SBE_CGAME_
Game.cpp
#include "base.h"
extern CGame* m_gGame;
CGame::CGame()
{
//
}
~CGame::CGame()
{
//
}
public void CGame::DoLoop()
{
SwapBuffers(hDC);
}
Base.h
#include <windows.h> // Header File For Windows ==NEEDS TO COME BEFORE GL.H AND GLU.H==
#include <gl\gl.h>
#include <gl\glu.h>
#include "Properties.h"
#include "Game.h"
#include "Renderer.h"
#ifndef _SBE_BASE_
#define _SBE_BASE_
extern CGame* m_gGame;
#endif //_SBE_BASE_
Globals.cpp
#include "base.h"
//=================================================================================
// Here is where we define all the global variables
//=================================================================================
CGame* m_gGame = new CGame();
What am I overlooking? I will admit, its been a while since I've programmed C++, but I reread class definition articles and all sorts of things. I have this not-so-strange feeling that its going to be something very silly, that I should have seen.
In Game.cpp:
~CGame::CGame()
should be
CGame::~CGame()
And drop the public keyword on the definition of CGame::DoLoop.
You need to write CGame::~CGame() instead of ~CGame::CGame() for the destructor. It's always the class name first (CGame) and only then the member name (~CGame).
#include <windows.h> // Header File For Windows ==NEEDS TO COME BEFORE GL.H AND GLU.H==
#include <gl\gl.h>
#include <gl\glu.h>
#include "Properties.h"
#include "Game.h"
#include "Renderer.h"
#ifndef _SBE_BASE_
#define _SBE_BASE_
extern CGame* m_gGame;
#endif //_SBE_BASE_
Why are you only include-guarding part of this file?
#ifndef _SBE_BASE_
#define _SBE_BASE_
#include <windows.h> // Header File For Windows ==NEEDS TO COME BEFORE GL.H AND GLU.H==
#include <gl\gl.h>
#include <gl\glu.h>
#include "Properties.h"
#include "Game.h"
#include "Renderer.h"
extern CGame* m_gGame;
#endif //_SBE_BASE_
Anyway, my guess is something weird in Properties.h or Renderer.h
you have mis-declared your destructor
it should be
CGame::~CGame()