I compile the following code but I get a compile error in Visual Studio that I cannot understand.
#include <iostream>
using namespace std;
int main()
{
int matchCount, findResult;
long childPID;
string userInput = "blank";
// string to be searched through
string longString = "The PPSh-41 is a Soviet submachine gun designed by Georgi Shpagin as an inexpensive, simplified alternative to the PPD-40.";
while (userInput.compare("!wq"));
{
// reset variables for reuse
matchCount = 0;
findResult = -1;
cout << "Please enter a word/s to search for (!wq to exit): "; // prompts user for string to search for
cin >> userInput; // takes user input
if (userInput.compare("!wq")) // checks user input to see if they still wish to search for a string
{
childPID = fork();
if (childPID == 0)
{
while (findResult < longString.length)
{
findResult = longString.find(userInput, findResult + 1, userInput.length);
if (findResult < longString.length)
matchCount++;
}
cout << "There are " << matchCount << " instances of " << userInput << " in longString." << endl;
}
else
cout << "childPID != 0" << endl;
}
else
cout << "User has chosen to exit. Exiting." << endl;
}
return 0;
}
The error reads:
«wordcount.cpp(57) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add ‘#include «stdafx.h»‘ to your source?»
I don’t believe I need a header file to run this code. Thank you for all your help in advance.
asked Nov 21, 2013 at 5:35
5
Look at https://stackoverflow.com/a/4726838/2963099
Turn off pre compiled headers:
Project Properties -> C++ -> Precompiled Headers
set Precompiled Header
to "Not Using Precompiled Header"
.
answered Nov 21, 2013 at 5:47
Glenn TeitelbaumGlenn Teitelbaum
10.1k3 gold badges36 silver badges81 bronze badges
3
The first line of every source file of your project must be the following:
#include <stdafx.h>
Visit here to understand Precompiled Headers
answered Nov 21, 2013 at 5:48
asifasif
9758 silver badges16 bronze badges
4
Create a new «Empty Project» , Add your Cpp file to the new project, delete the line that includes stdafx.
Done.
The project no longer needs the stdafx. It is added automatically when you create projects with installed templates.
answered Apr 22, 2014 at 2:26
Zahid RoufZahid Rouf
1,6112 gold badges12 silver badges10 bronze badges
1
I compile the following code but I get a compile error in Visual Studio that I cannot understand.
#include <iostream>
using namespace std;
int main()
{
int matchCount, findResult;
long childPID;
string userInput = "blank";
// string to be searched through
string longString = "The PPSh-41 is a Soviet submachine gun designed by Georgi Shpagin as an inexpensive, simplified alternative to the PPD-40.";
while (userInput.compare("!wq"));
{
// reset variables for reuse
matchCount = 0;
findResult = -1;
cout << "Please enter a word/s to search for (!wq to exit): "; // prompts user for string to search for
cin >> userInput; // takes user input
if (userInput.compare("!wq")) // checks user input to see if they still wish to search for a string
{
childPID = fork();
if (childPID == 0)
{
while (findResult < longString.length)
{
findResult = longString.find(userInput, findResult + 1, userInput.length);
if (findResult < longString.length)
matchCount++;
}
cout << "There are " << matchCount << " instances of " << userInput << " in longString." << endl;
}
else
cout << "childPID != 0" << endl;
}
else
cout << "User has chosen to exit. Exiting." << endl;
}
return 0;
}
The error reads:
«wordcount.cpp(57) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add ‘#include «stdafx.h»‘ to your source?»
I don’t believe I need a header file to run this code. Thank you for all your help in advance.
asked Nov 21, 2013 at 5:35
5
Look at https://stackoverflow.com/a/4726838/2963099
Turn off pre compiled headers:
Project Properties -> C++ -> Precompiled Headers
set Precompiled Header
to "Not Using Precompiled Header"
.
answered Nov 21, 2013 at 5:47
Glenn TeitelbaumGlenn Teitelbaum
10.1k3 gold badges36 silver badges81 bronze badges
3
The first line of every source file of your project must be the following:
#include <stdafx.h>
Visit here to understand Precompiled Headers
answered Nov 21, 2013 at 5:48
asifasif
9758 silver badges16 bronze badges
4
Create a new «Empty Project» , Add your Cpp file to the new project, delete the line that includes stdafx.
Done.
The project no longer needs the stdafx. It is added automatically when you create projects with installed templates.
answered Apr 22, 2014 at 2:26
Zahid RoufZahid Rouf
1,6112 gold badges12 silver badges10 bronze badges
1
HerMak 1 / 0 / 1 Регистрация: 26.12.2017 Сообщений: 35 |
||||
1 |
||||
05.03.2018, 16:45. Показов 9689. Ответов 5 Метки нет (Все метки)
Решил попробовать сделать змейку на чистом с++, но тут какая то ошибка, игру разрабатываю в Visual studio 2017 код ошибки С1010
0 |
3434 / 2813 / 1249 Регистрация: 29.01.2016 Сообщений: 9,426 |
|
05.03.2018, 16:46 |
2 |
Добавлял данную библиотеку-не помогло. Куда добавлял? Как добавлял? В коде ничего такого не наблюдаю. И это не библиотека.
0 |
1 / 0 / 1 Регистрация: 26.12.2017 Сообщений: 35 |
|
05.03.2018, 16:50 [ТС] |
3 |
Не библиотеку, извиняюсь, а вот такой строчкой
0 |
3434 / 2813 / 1249 Регистрация: 29.01.2016 Сообщений: 9,426 |
|
05.03.2018, 16:52 |
4 |
а вот такой строчкой Код со строчкой покажи.
0 |
HerMak 1 / 0 / 1 Регистрация: 26.12.2017 Сообщений: 35 |
||||
05.03.2018, 16:52 [ТС] |
5 |
|||
0 |
nd2 3434 / 2813 / 1249 Регистрация: 29.01.2016 Сообщений: 9,426 |
||||
05.03.2018, 16:55 |
6 |
|||
Сообщение было отмечено HerMak как решение Решение
Это выше всех инклудов должно быть.
1 |
I have a project, originally developed on Microsoft Visual C++ . NET, Version 7.0.9466 and it works very well. I tried to use MS 2013 to run my project, and when I try to build the project, I have this error menssage:
error C1010: unexpected end of file while looking for precompiled header. Did you forget to add ‘#include «stdafx.h»‘ to your source?
Some actions that I already did:
- I Selected the option «Use(/Yu)» on Precompiled Header in PropertyPages-> C/C++ -> Precompiled Header
- I Set the Precompiled Header File to stdafx.h
- I put
#include "stdafx.h"
at the beginning of the .cpp file - I had to change
#include "stdafx.h"
to#include "../stdafx.h"
because I had this error message:
IntelliSense: cannot open source file «stdafx.h»
Here is the .cpp file where I have this message error
#include "../stdafx.h"
#include "timedmsgbox.h"
/////////////////////////////////////////////////////////////////////////
//
// CDlgTimedMessageBox
//
/////////////////////////////////////////////////////////////////////////
CMapPtrToPtr CDlgTimedMessageBox::m_mapTimerIdToClassMe;
CCriticalSection CDlgTimedMessageBox::m_sectMap;
extern BOOL AFXAPI AfxIsDescendant(HWND hWndParent, HWND hWndChild);
// the static one to call the messagebox with one line
UINT CDlgTimedMessageBox::TimedMessageBox(UINT flags, LPCTSTR ptszMessage, LPCTSTR ptszTitle,
DWORD dwTimeout, UINT dDefaultReturn,
LPCTSTR ptszMessageTimer, HWND hwndParent, BOOL *pbStoppedByUser)
{
CDlgTimedMessageBox msgBox(flags, ptszMessage, ptszTitle,
dwTimeout, dDefaultReturn,
ptszMessageTimer, hwndParent);
return msgBox.ShowMessageBox(pbStoppedByUser);
}
CDlgTimedMessageBox::CDlgTimedMessageBox(UINT flags,
LPCTSTR ptszMessage, LPCTSTR ptszTitle,
DWORD dwTimeout, UINT dDefaultReturn,
LPCTSTR ptszMessageTimer,
HWND hwndParent)
{
m_hParent = hwndParent;
m_Message = ptszMessage;
m_Title = ptszTitle;
m_flags = flags;
m_dwTimeout = dwTimeout-1;
m_MessageTimer = ptszMessageTimer;
m_DefaultReturn = dDefaultReturn;
m_hMsgBox = NULL;
m_hStaticText = NULL;
m_hDefaultButton = NULL;
m_bRunning = FALSE;
m_bStoppedByTimer = FALSE;
if( !m_hParent )
{
CWnd *m_pParent = AfxGetApp()->GetMainWnd();
m_hParent = m_pParent->m_hWnd;
}
}
CDlgTimedMessageBox::~CDlgTimedMessageBox()
{
}
#pragma warning( push)
#pragma warning (disable : 4312) // conversion to type of greater size
UINT CDlgTimedMessageBox::ShowMessageBox(BOOL *pbStoppedByUser)
{
// start timer
CDlgTimedMessageBox::m_sectMap.Lock();
{
m_idTimer = (UINT)::SetTimer(NULL, 0, 1000, (TIMERPROC) CDlgTimedMessageBox::GlobalTimerProc);
CDlgTimedMessageBox::m_mapTimerIdToClassMe.SetAt((void*)m_idTimer, this);
}
CDlgTimedMessageBox::m_sectMap.Unlock();
// show MessageBox
m_bRunning = TRUE;
m_dwStarted = ::GetTickCount();
m_CurrentMessage = m_Message;
if( !m_MessageTimer.IsEmpty() )
{
CString second;
second.Format(m_MessageTimer, (m_dwTimeout+1)/1000);
m_CurrentMessage.Format("%s%s", m_Message, second);
}
UINT erg = ::MessageBox(m_hParent, m_CurrentMessage, m_Title, m_flags);
m_bRunning = FALSE;
CDlgTimedMessageBox::m_sectMap.Lock();
{
::KillTimer(NULL, m_idTimer);
m_idTimer = 0;
CDlgTimedMessageBox::m_mapTimerIdToClassMe.RemoveKey((void*)m_idTimer);
}
CDlgTimedMessageBox::m_sectMap.Unlock();
if( pbStoppedByUser )
*pbStoppedByUser = !m_bStoppedByTimer;
return erg;
}
#pragma warning( pop )
void CALLBACK CDlgTimedMessageBox::GlobalTimerProc(HWND hwnd, UINT uiMsg, UINT_PTR idEvent, DWORD dwTime)
{
//TRACE("Global timer with id=%u\n", idEvent);
CDlgTimedMessageBox *pMe = NULL;
// Find the corresponding class by the timer-id
CDlgTimedMessageBox::m_sectMap.Lock();
{
CDlgTimedMessageBox::m_mapTimerIdToClassMe.Lookup((void*)idEvent, (void *&) pMe);
}
CDlgTimedMessageBox::m_sectMap.Unlock();
if( pMe!=NULL )
pMe->LocalTimerProc();
}
void CDlgTimedMessageBox::LocalTimerProc(void)
{
//TRACE("Local timer with id=%u (%s)\n", m_idTimer, m_Title);
if( !m_bRunning )
return;
// lookup the handles
GetWindowHandles();
if( !m_hStaticText || !m_hMsgBox )
return;
DWORD now = GetTickCount()-m_dwStarted;
if( now >= (m_dwTimeout) )
{
// done with the box
m_bStoppedByTimer = TRUE;
::PostMessage(m_hMsgBox, WM_COMMAND, (WPARAM) m_DefaultReturn, (LPARAM) m_hDefaultButton);
}
else
{
m_CurrentMessage = m_Message;
// not done: set text again
if( !m_MessageTimer.IsEmpty() )
{
CString second;
second.Format(m_MessageTimer, (100+m_dwTimeout-now)/1000);
m_CurrentMessage.Format("%s%s", m_Message, second);
}
::SetWindowText(m_hStaticText, m_CurrentMessage);
}
}
void CDlgTimedMessageBox::GetWindowHandles(void)
{
HWND hWnd;
CWnd *pWnd;
CString title;
CPtrList allButtons;
//
// Handle of the messageBox
//
if( !m_hMsgBox )
{
hWnd = ::GetWindow(::GetDesktopWindow(), GW_CHILD);
while( (hWnd!=NULL) && (m_hMsgBox==NULL) )
{
pWnd = CWnd::FromHandle(hWnd);
pWnd->GetWindowText(title);
if( AfxIsDescendant(m_hParent, hWnd) && ::IsWindowVisible(hWnd) && (m_Title.CompareNoCase(title)==0) )
{
m_hMsgBox = hWnd;
break;
}
hWnd = ::GetWindow(hWnd, GW_HWNDNEXT);
}
}
//
// Handle of the static text
// TODO only if text-replace is needed
//
if( m_hMsgBox && !m_hStaticText )
{
// not sure if this will work always
// under Win2000 it did
//m_hStaticText = ::GetDlgItem(m_hMsgBox, 0xFFFF);
// not sure, so lets find it dynamically!
char className[_MAX_PATH];
CString classNameOk("STATIC");
LONG id;
hWnd = ::GetWindow(m_hMsgBox, GW_CHILD);
while( (hWnd!=NULL) && (m_hStaticText==NULL) )
{
id = ::GetWindowLong(hWnd, GWL_ID);
// small ids only for buttons
if( id > IDHELP )
{
if( ::GetClassName(hWnd, className, _MAX_PATH) )
{
// looking only for a static
if( classNameOk.CompareNoCase(className) == 0 )
{
// not check the text
pWnd = CWnd::FromHandle(hWnd);
pWnd->GetWindowText(title);
if( m_CurrentMessage.CompareNoCase(title) == 0 )
{
m_hStaticText = hWnd;
break;
}
}
}
}
else
{
allButtons.AddTail(hWnd);
}
hWnd = ::GetWindow(hWnd, GW_HWNDNEXT);
}
}
//
// Handle of the default button
//
if( m_hMsgBox && !m_hDefaultButton )
{
m_hDefaultButton = ::GetDlgItem(m_hMsgBox, m_DefaultReturn);
// Problem:
// if generated with MB_OK the button has NOT IDOK, but IDCANCEL !!
// then lets take the first button we find !
// (with and IDCANCEL this works, because it is the only button
// if this problem encounters also with 2 buttons, I have no chance
// to find out which one is the better one!)
while( allButtons.GetCount()>0 && !m_hDefaultButton )
{
m_hDefaultButton = (HWND) allButtons.GetHead();
allButtons.RemoveHead();
if( m_hDefaultButton )
m_DefaultReturn = ::GetWindowLong(m_hDefaultButton, GWL_ID);
}
}
}
and here is the stdafx.h file
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently
#pragma once
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#endif
// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER // Allow use of features specific to Windows 95 and Windows NT 4 or later.
//#define WINVER 0x0400 // Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#define WINVER 0x0602 // SMS2014XXXX alteração para funcionar windows 8
#endif
#ifndef _WIN32_WINNT // Allow use of features specific to Windows NT 4 or later.
//#define _WIN32_WINNT 0x0400 // Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#define _WIN32_WINNT 0x0602 // SMS2014XXXX alteração para funcionar windows 8
#endif
#ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif
#ifndef _WIN32_IE // Allow use of features specific to IE 4.0 or later.
#define _WIN32_IE 0x0400 // Change this to the appropriate value to target IE 5.0 or later.
#endif
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
// turns off MFC's hiding of some common and often safely ignored warning messages
#define _AFX_ALL_WARNINGS
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#include <afxdhtml.h>
I am new on visual studio and I have no ideia what’s is wrong on all of this. Someone could help me with it?
- Remove From My Forums
-
Question
-
fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add ‘#include «stdafx.h»‘ to your source?
I am using Visual Studio 2005 Academic Edition.
I clicked «Tool»=>Options=>Debugging=>Edit and Continue.
Let «Allow precomping» unchecked then.
Why does this error occury?
Thanks!
Answers
-
«Did you forget to add #include «stdafx.h» to your source»?
All replies
-
«Did you forget to add #include «stdafx.h» to your source»?
-
I have generated source code and was trying to compile in release mode (instead of debug). I got this error. Putting in the #include «stdafx.h» does not fix the problem. Anyone know what is going on and why I am getting this error in Release mode and not in Debug mode?
-
I figured out what the problem was. The entire project needed to be marked as ‘Not Using Precompiled Headers’ in the property pages. This is under the ‘Configuration Properties’->C/C++->’Precompiled Headers’. You could just single out the one file if you wanted to also.
I then was getting some linker unresolved external errors after doing that. Make sure you also check for any libraries that need to be linked against. I had (NOINHERIT) in my Additional Dependencies field of the properties. That field is under the ‘Configuration Properties’->Linker->Input.
Just wanted to put on the board what I found in case anyone else has these easy setup issues. These things are different from what I am used to or at least in different places.
-
Proposed as answer by
Friday, October 7, 2011 7:40 PM
-
Proposed as answer by
-
Where is
‘Configuration Properties’?
Thanks
-
Proposed as answer by
Prizzy29
Monday, October 26, 2009 7:27 AM
-
Proposed as answer by
-
The Steve340 wrote: I have generated source code and was trying to compile in release mode (instead of debug). I got this error. Putting in the #include «stdafx.h» does not fix the problem. Anyone know what is going on and why I am getting this error in Release mode and not in Debug mode?
-
Once the C++ is open in a project = Tools/options/”edit/countine” you click on the left side / and then close to the bottom is the option to add or remove ‘Precompiled Headers’
I had an issue, where there ‘Precompiled Headers’ came out of no where, I didn’t even chose it but, I googled the issue and came up with the site. An I read the idea’s on here an searched for it in C++ search and it told me how to do. An I thought I would share the little info.
-
Proposed as answer by
SANJAY KHACHANE
Wednesday, June 6, 2012 10:58 AM -
Unproposed as answer by
SANJAY KHACHANE
Wednesday, June 6, 2012 10:59 AM -
Proposed as answer by
SANJAY KHACHANE
Wednesday, June 6, 2012 10:59 AM
-
Proposed as answer by
-
Go To
I am using Visual Studio 2010.
Clicked New Project —> Visual C++ —> Win32 Console Application —>
Enter Name Of Application—> Click Ok
Show «Win32 Application Wizard» — > Click Next —>
In Scrine Show
Additional options:
Empty project
Export symbols
Precompiled headerUntick Precompiled Header
then Finished It
-
Edited by
SANJAY KHACHANE
Wednesday, June 6, 2012 11:08 AM
-
Edited by
-
Hi..ardmore,
I was having same problem, but solved it by following manner..
Open property of that particular page and go to confi. Property -> c/c++ -> precompiled headers -> set this value to “Not using precompiled Headers”
-
Edited by
Mayur.Dabhi
Thursday, September 20, 2012 11:06 AM
-
Edited by
-
Thank you soo much.. its works fine steve