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.)
Related
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).
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!
I'm trying to compile a simple C++ program that uses some functions and datastructures from the Win32 API and Wincrypt:
#include <memory>
using std::unique_ptr;
#include <Windows.h>
#include <Wincrypt.h>
using CERTSTORE_ptr = unique_ptr<CERTSTORE, decltype(&:: CertCloseStore)>;
int main(int argc, char* argv[])
{
return 0;
}
When I attempt to compile it, I get errors at the using CERTSTORE_ptr ... line. The errors:
test.cpp(18): error C2873: 'CERTSTORE_ptr' : symbol cannot be used in a using-declaration
test\certstoreexporttest.cpp(18): error C2513: 'int' : no variable declared before '='
test\certstoreexporttest.cpp(18): error C2065: 'CERTSTORE' : undeclared identifier
...
The problem appears to be with CERTSTORE and HCERTSTORE, which is typedef'd as:
typedef void *HCERTSTORE;
How do I declare the using statement so I can enjoy the automatic cleanup? (I'm trying to avoid the __try\__finally).
Or is this simply the wrong approach and should be abandoned? (I'm ready to go back to __try\__finally so I can finish up this test program).
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 got a strange compilation error when I followed the MSDN document to use CA2W to convert big5 strings to unicode strings in Visual Studio 2005.
This is the code I wrote:
#include <string>
#include <atldef.h>
#include <atlconv.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string chineseInBig5 = "\xA4\xA4\xA4\xE5";
ATL::CA2W(chineseInBig5.c_str());
return 0;
}
The compilation error: error C3861: 'AtlThrowLastWin32': identifier not found
I don't know how this could happen. The document of AtlThrowLastWin32 shows that atldef.h is required, but I couldn't find the declaration of AtlThrowLastWin32 in atldef.h.
I finally solved this problem by adding 2 include headers:
#include <atlbase.h>
#include <atlstr.h>
I don't know why the MSDN document doesn't mention that.