Am trying to include CString in my cpp file but am getting this error if i include afxinet.h
"windows.h already included afxv_w32.h"
These are my header files :
#include stdafx.h
#include shlwapi.h
#include Tlhelp32.h
#include hooks.h
#include stdio.h
#include common1.h
#include SafeLogger.h
#include io.h
#include tinyxml.h
#include winsock.h>
#pragma comment(lib, "Ws2_32.lib")
#include afxinet.h
I havent included the gaurds in stack to display here.
how to solve this issue and get to include CString in my file
Why are you adding afxinet.h if you want CString?
You should include atlstr.h to get CString - particularly if your project isn't MFC based.
Use /showIncludes option in C++ settings (Under Advanced). This will show how headers are being included (in tree-format). As another suggestion, you should include only files that are needed.
By including < afx.h >, you should be able to use CString class in cpp file.
#include <afx.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CString str("Santosh");
wcout << LPCTSTR(str) << endl;
return 0;
}
Additionally, you will need to define _AFXDLL preprocessor
Related
I've included the TlHelp32.h header properly though I keep getting the message:
"Error: identifier "CreateToolhelp32Snapshot" is undefined"
when attempting to use CreateToolhelp32Snapshot. When I used the "peek definition" feature in VS I found that there are errors within this header where in certain areas it says:
"Error expected a ';'"
Any ideas how to fix this?
#include "stdafx.h"
#include <TlHelp32.h>
#include <Windows.h>
#include <iostream>
using namespace std;
class Functions{
public:
void playerHealthPrinter(){
HANDLE hProcess;
DWORD dwPID, dwProtection, dwCaveAddress;
BOOL bPOn, bIOn, bProt;
HANDLE hPID = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
Never include Win32 headers before <windows.h>. The following should work, instead:
#include <Windows.h>
#include <TlHelp32.h> // <-- include *after* windows.h
If it still doesn't work then please post an MCVE including the relevant headers and version of VS.
I'm a bit confused at the moment because I'm planning to include multiple source and header files for the first time in one of my projects.
So I'm wondering if this would be the right approach?
Do I have to include the string header in every source file that uses it directly?
And what about the "stdafx.hpp" header that Visual C++ wants me to include?
Would that be the way to go?
main.cpp
#include "stdafx.hpp"
#include <string> //?
#include <stringLib1.h>
#include <stringLib2.h>
using std::string;
//use a windows.h function here
//use a stringLib1 function here
//use a stringLib2 function here
stringLib1.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass1
{
public:
string GetStringBack1(string myString);
};
stringLib1.cpp
#include "stdafx.hpp"
string uselessClass1::GetStringBack1(string myString) {
return myString;
}
stringLib2.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass2
{
public:
string GetStringBack2(string myString);
};
stringLib2.cpp
#include "stdafx.hpp"
string uselessClass2::GetStringBack2(string myString) {
return myString;
}
A good practice is usually to include only what your code uses in every file. That reduces dependencies on other headers and, on large projects, reduce compilation times (and also helps finding out what depends on what)
Use include guards in your header files
Don't import everything by polluting the global namespace, e.g.
using namespace std;
but rather qualify what you intend to use when you need it
You don't need stdafx.h in your project unless you're using precompiled headers. You can control this behavior in the VS project properties (C/C++ -> Precompiled Headers -> Precompiled Header)
The stdafx.h header is needed if precompiled header is enabled in VS. (Read this one)
You only need to include the stdafx.h in your .cpp files as the first include.
Regarding the header and cpp files (which come in pairs), include things necessary for the declaration in the header, and include everything else (necessary for the definition) in the cpp. Also include the corresponding header in its cpp pair too. And use include guards.
myclass.h
#ifndef MYCLASS_H // This is the include guard macro
#define MYCLASS_H
#include <string>
using namespace std;
class MyClass {
private:
string myString;
public:
MyClass(string s) {myString = s;}
string getString(void) {return myString;}
void generate();
}
myclass.cpp
#include <stdafx.h> // VS: Precompiled Header
// Include the header pair
#include "myclass.h" // With this one <string> gets included too
// Other stuff used internally
#include <vector>
#include <iostream>
void MyClass::generate() {
vector<string> myRandomStrings;
...
cout << "Done\n";
}
#endif
Then in main(...), you can just include myclass.h and call generate() function.
The stdafx include should be at the top of every .cpp file and it should NOT be in .h files.
You could put #include < string > in stdafx.h if you don't want to put it in every other file.
I suppose that you must be having your own header files also which might be requiring in other cpp files and header files. Like the one you gave
#include <stringLib1.h>
#include <stringLib2.h>
In my opinion, its better to create one common header file in which you include all the common library header files and your project header file. This file then you can include in all the other cpp files and header file. And it will be better to use header guards also.
So, considering a common header file "includes.h".
#ifndef INCLUDES_H
#define INCLUDES_H
#include <string>
#include <stringLib1.h>
#include <stringLib2.h>
/***Header files***/
#endif //INCLUDES_H
This is now your common header file. This you can include in all your project files.
I have created simple win32 console application:
#include "stdafx.h"
#include <iostream>
#include "conio.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello World" << endl;
int num;
cin >> num;
return 0;
}
It compiles fine.
Then I'm trying to add library. I Have dll lib and .h files.
I have included my.h file:
#include "stdafx.h"
#include "my.h"
#include <iostream>
#include "conio.h"
"my.h" contains lines:
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
After compile I got error:
include 'stdafx.h' before including this file for PCH
But I have already included 'stdafx.h'. I have tested with both options of Use/Not use precompile headers - the same result. Where is the problem?
Your my.h is using MFC (__AFXWIN_H__ is defined by MFC headers) but your console program is not. You must use MFC in your program or rewrite your library to not use MFC.
Do not include external headers or stdafx.h in your headers.
Include all external headers in stdafx.h.
Include stdafx.h from every cpp file in your project.
This will build faster too as the external headers are processed once via PCH.
I'm a little confused at why this is and i'm sure it's a basic thing I should know if i'm programming in C++ but here is the question:
I have a "Windows.cpp" and at the top it has includes of
#include <windows.h>
#include "Game.h"
#include "Mouse.h"
#include "Screen.h"
...
In my Screen.h I have the following which obviously requires information from windows.h because of the use of DWORD:
#pragma once
#include <windows.h>
class ScreenServer;
class ScreenClient
{
public:
ScreenClient( const ScreenServer &server );
DWORD GetScreenHeight();
DWORD GetScreenWidth();
...
The question is, why do I have to #include windows.h within Screen.h, when my "Windows.cpp" already has included it before "Screen.h" is included?
Short answer:
Because some other file that doesn't #include <windows.h> might include Screen.h.
A bit longer:
In general, you should always include the headers you need, where you need them, and not rely on them being included somewhere else. Use forward declarations where possible, but if you need a full type, include the header.
//head.h//
extern int sum(int,int);
//head.cpp//
#include "head.h"
#include "stdafx.h"
int sum(int x, int y)
{
return (x+y);
}
//mainfn.cpp//
#include "head.h"
#include "stdafx.h"
#include string
#include iostream
#include stdio.h
using std::string;
using std::cout;
using namespace System;
int main()
{
int x=10,y=2;
printf("value: %d",sum(x,y));
Console::ReadLine();
return 0;
}
While buliding in Visual studio 2005, this vc++ project is giving following error:
error C3861: 'sum': identifier not found.
Can anybody help me out with this?
You need to place the inclusion of head.h after stdafx.h. When precompiled headers are enabled the compiler will ignore the contents of all includes that occur prior to (in this case) the inclusion of stdafx.h .
Either remove stdafx.h from the project, and turn of precompiled headers.. or try moving head.h to be included after stdafx.h instead of before.