When I compiled this program (from C++ Programming Language 4th edition):
main.cpp
#include <stdafx.h>
#include <iostream>
#include <cmath>
#include "vector.h"
using namespace std;
double sqrt_sum(vector&);
int _tmain(int argc, _TCHAR* argv[])
{
vector v(6);
sqrt_sum(v);
return 0;
}
double sqrt_sum(vector& v)
{
double sum = 0;
for (int i = 0; i != v.size(); ++i)
sum += sqrt(v[i]);
return sum;
}
vector.cpp
#include <stdafx.h>
#include "vector.h"
vector::vector(int s)
:elem{ new double[s] }, sz{ s }
{
}
double& vector::operator[](int i)
{
return elem[i];
}
int vector::size()
{
return sz;
}
vector.h
#include <stdafx.h>
class vector{
public:
vector(int s);
double& operator[](int i);
int size();
private:
double* elem;
int sz;
};
It gave me these errors:
I run it on Microsoft Visual Studio 2013, on Windows 7. How to fix it?
asked Oct 12, 2014 at 21:28
1
You have to properly understand what is a «stdafx.h», aka precompiled header. Other questions or Wikipedia will answer that. In many cases a precompiled header can be avoided, especially if your project is small and with few dependencies. In your case, as you probably started from a template project, it was used to include Windows.h
only for the _TCHAR
macro.
Then, precompiled header is usually a per-project file in Visual Studio world, so:
- Ensure you have the file «stdafx.h» in your project. If you don’t (e.g. you removed it) just create a new temporary project and copy the default one from there;
- Change the
#include <stdafx.h>
to#include "stdafx.h"
. It is supposed to be a project local file, not to be resolved in include directories.
Secondly: it’s inadvisable to include the precompiled header in your own headers, to not clutter namespace of other source that can use your code as a library, so completely remove its inclusion in vector.h
.
answered Oct 12, 2014 at 21:42
ceztkoceztko
14.6k5 gold badges56 silver badges73 bronze badges
5
Just include windows.h instead of stdfax or create a clean project without template.
answered Oct 12, 2014 at 22:05
user3718058user3718058
2931 gold badge3 silver badges11 bronze badges
There are two solutions for it.
Solution number one:
1.Recreate the project. While creating a project ensure that precompiled header is checked(Application settings… *** Do not check empty project)
Solution Number two:
1.Create stdafx.h and stdafx.cpp in your project
2 Right click on project -> properties -> C/C++ -> Precompiled Headers
3.select precompiled header to create(/Yc)
4.Rebuild the solution
Drop me a message if you encounter any issue.
answered Jun 11, 2016 at 6:51
Dila GurungDila Gurung
1,67821 silver badges25 bronze badges
1
You can fix this problem by adding «$(ProjectDir)» (or wherever the stdafx.h is) to list of directories under Project->Properties->Configuration Properties->C/C++->General->Additional Include Directories.
answered Sep 13, 2018 at 15:46
SarSar
1151 silver badge6 bronze badges
Add #include "afxwin.h"
in your source file. It will solve your issue.
Al Lelopath
6,37813 gold badges82 silver badges139 bronze badges
answered Jun 29, 2015 at 10:29
Dila GurungDila Gurung
1,67821 silver badges25 bronze badges
Just running through a Visual Studio Code tutorial and came across a similiar issue.
Replace #include "stdafx.h"
with #include "pch.h"
which is the updated name for the precompiled headers.
Marius
1,5096 silver badges20 bronze badges
answered Dec 26, 2018 at 13:07
0 / 0 / 0 Регистрация: 29.12.2014 Сообщений: 7 |
|||||
1 |
|||||
13.01.2015, 16:41. Показов 22770. Ответов 7
скажите пожалуйста как исправит эту ошибку
0 |
117 / 114 / 65 Регистрация: 18.09.2014 Сообщений: 337 |
|
13.01.2015, 16:43 |
2 |
Хигмат, а он у вас вообще есть, этот файл?
0 |
8725 / 4305 / 958 Регистрация: 15.11.2014 Сообщений: 9,752 |
|
13.01.2015, 16:44 |
3 |
Удалить физически строку с подключением ненужного вам «предварительно скомпилированного заголовка». IDE генерирует файлы проекта с использованием/не использованием «предварительно скомпилированных заголовков» при создании проекта. Если поставите галочку «использовать предварительно скомпилированный заголовок» — студия создаст для вас необходимый хэдэр, который будет необходимо подключать к каждому cpp файлу. Но если такую галочку не ставить — то и не нужно пытаться цеплять несуществующий хэдэр.
0 |
0 / 0 / 0 Регистрация: 29.12.2014 Сообщений: 7 |
|
13.01.2015, 16:55 [ТС] |
4 |
?????? Добавлено через 1 минуту Добавлено через 44 секунды
0 |
7529 / 6394 / 2914 Регистрация: 14.04.2014 Сообщений: 27,855 |
|
13.01.2015, 17:06 |
5 |
Пересоздай проект с параметрами по умолчанию и перенеси туда текст.
0 |
Модератор 13245 / 10387 / 6210 Регистрация: 18.12.2011 Сообщений: 27,784 |
|
13.01.2015, 17:21 |
6 |
Отключить заголовки, меню:
0 |
Хигмат 0 / 0 / 0 Регистрация: 29.12.2014 Сообщений: 7 |
||||
13.01.2015, 17:29 [ТС] |
7 |
|||
помогите переписать
0 |
zss Модератор 13245 / 10387 / 6210 Регистрация: 18.12.2011 Сообщений: 27,784 |
||||
13.01.2015, 17:36 |
8 |
|||
Отключите заголовки, как я написал выше
0 |
- Remove From My Forums
-
Question
-
Hi,
I need help on how to compile https://www.exploit-db.com/exploits/45805/
What steps I miss
tqSW INFO:
-Visual Studio Community 2017
-https://www.exploit-db.com/exploits/45805/ERROR:
cannot open source file «stdafx.h»
cannot open source file «resource.h»
identifier «IDR_DATA1» is undefinedSTEPS TAKEN
create a New Project
click Visual C++/Windows Desktop/Windows Desktop Wizard
tick
Precompiled Header
Security Dev…
click menu Project/Add existing items
add 45805.cpp
right click Project/Build
then got error above-
Edited by
Monday, November 19, 2018 8:12 AM
-
Moved by
Sara LiuMicrosoft contingent staff
Monday, November 19, 2018 8:56 AM
-
Edited by
To my knowledge it a pre-compiled header file, It helps in speeding up the C++ application. Notice the double quotes «» over there that means it is a user added header file generally headers will be in <> and for user added header file #include»location of the header file» stdafx is generally added by the compiler in a default project location and for an empty project the compiler wont create any files at all so you need to add New Item(Header file) with the name of stdafx.h to make the application work for you. Remember this will not speed up the C++ application.
The proper way of adding stdafx.h is by mentioning use pre-compiled header file in your Project settings.
Feel free to ask any doubts
please help me….
me all the time that error occurs, look after the forums but not found out what this is about
this is code:
#include «stdafx.h»
#include <iostream>
/*
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include «randomcard.h»
*/
# define CLANOVI_OBITELJI 100
# define MJESECI 100
using namespace std;
int main(){
float dzeparci[CLANOVI_OBITELJI][MJESECI];
float dzeparci_po_clanu[CLANOVI_OBITELJI];
float dzeparci_po_mjesecu[MJESECI];
int br_clanova,br_mjeseci;
cout<<«Unesi broj clanova obitlji: «;
cin>>br_clanova;
cout<<«Unesi broj mjeseci: «;
cin>>br_mjeseci;
for(int i=0; i<br_clanova; i++){
cout<<«n»<<i+1<<«. CLAN OBITELJI»<<endl;
for(int j=0; j<br_mjeseci; j++){
cout<<j+1<<«. mjesec: «;
cin>>dzeparci[i][j];
}
}
float zbroj_dzeparca=0;
for(int i=0; i<br_clanova; i++){
zbroj_dzeparca=0;
for(int j=0; j<br_mjeseci; j++){
zbroj_dzeparca += dzeparci[i][j];
}
dzeparci_po_clanu[i] = zbroj_dzeparca/br_mjeseci;
}
for(int i=0; i<br_mjeseci; i++){
zbroj_dzeparca=0;
for(int j=0; j<br_clanova; j++){
zbroj_dzeparca += dzeparci[j][i];
}
dzeparci_po_mjesecu[i] = zbroj_dzeparca/br_clanova;
}
cout<<«DZEPARCI»<<endl;
for(int i=0; i<br_clanova; i++){
cout<<i+1<<«. CLAN:»;
for(int j=0; j<br_mjeseci; j++){
cout<<«t»<<dzeparci[i][j];
}
cout<<endl;
}
cout<<«PROSJECNI DZEPARAC PO CLANU OBITELJI»<<endl;
for(int i=0; i<br_clanova; i++){
cout<<i+1<<«. CLAN:t»<<dzeparci_po_mjesecu[i]<<endl;
}
return 0;
}
thx