C++ #include 在查找预编译头使用时被跳过——在查找预编译头时意外的文件结尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8483472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
#include skipped when looking for precompiled header use -- unexpected end of file while looking for precompiled header
提问by Иван Бишевац
I have some code but it wan't compile. I can't go further with this example because this error raises every time I try to debug. Note: I am Java programmer, and this message in VS is strange and I coudn't find what is problem. Can someone explain me what this error mean?
我有一些代码,但它无法编译。我不能在这个例子中更进一步,因为每次我尝试调试时都会出现这个错误。注意:我是 Java 程序员,VS 中的这条消息很奇怪,我找不到问题所在。有人能解释一下这个错误是什么意思吗?
Here is code:
这是代码:
zadatak.cpp:
zadatak.cpp:
// zadatak.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "String.h"
#include <iostream>
using namespace std;
void main()
{
String s1(20),s2(20);
cout<<"Unesi prvi string."<<endl;
cin>>s1;
cout<<"Unesi drugi string."<<endl;
cin>>s2;
++s1;
s2++;
String s3(40);
s3=s1+s2;
cout<<"Treci string: "<<s3<<endl;
}
String.cpp:
字符串.cpp:
#include "String.h"
#include <iostream>
using namespace std;
#include <string.h>
String::String(void)
:duzina(0)
{
niz=new char[duzina];
}
String::String(int br)
:duzina(br)
{
this->niz=new char[duzina];
}
String::~String(void)
{
delete [] niz;
}
String::String(String& s)
:duzina(s.duzina)
{
this->niz=new char[this->duzina];
for(int i=0; i<this->duzina; i++)
this->niz[i]=s.niz[i];
}
void String::nadjiPocetak(char rec[])
{
int poc=0;
for(int i=0; i<this->duzina; i++)
{
int duz=0;
if(this->niz[i]==rec[0])
{
int l=strlen(rec);
for(int j=0; j<l; j++)
if(this->niz[i+j]==rec[j])
duz+=1;
}
if(duz==strlen(rec))
poc=i;
}
if(poc==0)
cout<<"U stringu ne postoji zadati string."<<endl;
else
cout<<"Pocetak je na poziciji: "<<poc+1<<endl;
}
/*void String::ucitaj()
{
cout<<"Unesi string"<<endl;
cin>>this->niz;
}
void String::prikazi()
{
cout<<"String: "<<this->niz<<endl;
}*/
String& String::operator+ (String s)
{
String *s1;
s1=new String(duzina+s.duzina);
strcpy(s1->niz,niz);
strcat(s1->niz,s.niz);
return *s1;
}
String& String::operator++ ()
{
if(niz[0]>='a' && niz[0]<='z')
niz[0]='A'+niz[0]-'a';
return *this;
}
String& String::operator++ (int n)
{
for(int i=0; i<duzina; i++)
if(niz[i]>='a' && niz[i]<='z')
niz[i]='A'+niz[i]-'a';
return *this;
}
String& String::operator= (String& s)
{
duzina=s.duzina;
delete [] niz;
niz=new char[duzina];
strcpy(niz,s.niz);
return *this;
}
istream& operator>> (istream& in, String& s)
{
in>>s.niz;
return in;
}
ostream& operator<< (ostream& out, String& s)
{
out<<s.niz;
return out;
}
String.h:
字符串.h:
#pragma once
#include <iostream>
using namespace std;
class String
{
private:
int duzina;
char* niz;
public:
String(void);
String(int br);
~String(void);
String(String& s);
int vratiDuzinu() {return this->duzina;}
void nadjiPocetak(char rec[]);
//void ucitaj();
//void prikazi();
String& operator+ (String s);
String& operator++ ();
String& operator++ (int n);
String& operator= (String& s);
friend istream& operator>> (istream& in, String& s);
friend ostream& operator<< (ostream& out, String& s);
};
Error that Visual Studio gives in output is:
Visual Studio 在输出中给出的错误是:
1>------ Build started: Project: zadatak, Configuration: Debug Win32------
1>Build started 12/13/2011 02:44:50 AM.
1>InitializeBuildStatus:
1> Touching "Debug\zadatak.unsuccessfulbuild".
1>ClCompile:
1> All outputs are up-to-date.
1> zadatak.cpp
1> String.cpp
1>c:\users\ivo\documents\visual studio 2010\projects\zadatak\zadatak\string.cpp(1): warning C4627: '#include "String.h"': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\ivo\documents\visual studio 2010\projects\zadatak\zadatak\string.cpp(2): warning C4627: '#include ': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\ivo\documents\visual studio 2010\projects\zadatak\zadatak\string.cpp(4): warning C4627: '#include ': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\ivo\documents\visual studio 2010\projects\zadatak\zadatak\string.cpp(108): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
1> Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.41
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1>------ 构建开始:项目:zadatak,配置:调试 Win32------
1>构建开始于 12/13/2011 02:44:50 AM。
1>InitializeBuildStatus:
1> 触摸“Debug\zadatak.unsuccessfulbuild”。
1>ClCompile:
1> 所有输出都是最新的。
1> zadatak.cpp
1> String.cpp
1>c:\users\ivo\documents\visual studio 2010\projects\zadatak\zadatak\string.cpp(1): warning C4627: '#include "String.h"' : 在查找预编译头文件时跳过使用
1> 添加指令到 'StdAfx.h' 或重建预编译头文件
1>c:\users\ivo\documents\visual studio 2010\projects\zadatak\zadatak\string.cpp(2):警告 C4627:“#include”:
1> 向'StdAfx.h' 添加指令或重建预编译头文件
1>c:\users\ivo\documents\visual studio 2010\projects\zadatak\zadatak\string.cpp(4): warning C4627: '#include ':查找预编译头文件时跳过使用
1> Add directive to 'StdAfx.h' 或重建预编译头文件
1>c:\users\ivo\documents\visual studio 2010\projects\zadatak\zadatak\string.cpp(108): fatal错误 C1010:查找预编译头时文件意外结束。您是否忘记在源代码中添加“#include "StdAfx.h"”?
1> 生成代码...
1>
1> 构建失败。
1>
1> Time Elapsed 00:00:00.41
========== 构建:0 成功,1 失败,0 最新,
采纳答案by John
You want
你要
#include "stdafx.h"
in your String.cpp file, as with all .cpp files.
在您的 String.cpp 文件中,与所有 .cpp 文件一样。
回答by Jesse Good
You using the default MSVC project which includes precompiled headers. I would recommend selecting "Dont use precompiled headers option" when you make a project.
您使用包含预编译头文件的默认 MSVC 项目。我建议您在创建项目时选择“不要使用预编译头选项”。
回答by Nenad Bulatovic
You should add
你应该添加
#include "stdafx.h"
in your String.cpp file, but if you used precompiled headers you have to put it on the TOP, such as this:
在你的 String.cpp 文件中,但如果你使用了预编译的头文件,你必须把它放在 TOP 上,例如:
#include "stdafx.h"
#include "String.h"
#include <iostream>
using namespace std;
#include <string.h>
There is extensive discussion herewhy precompiled headers must come first.
这里有广泛的讨论 ,为什么必须首先使用预编译头文件。
Everything before the PCH is ignored by the compiler, therefore PCH must come first.
PCH 之前的所有内容都会被编译器忽略,因此 PCH 必须排在第一位。
回答by Marina Lans
VS 2013 > Alt+F7 > Configuration Properties > C/C++ > All Options > clean option in "SDL checks" > Apply > OK.
VS 2013 > Alt+F7 > 配置属性 > C/C++ > 所有选项 > “SDL 检查”中的清理选项 > 应用 > 确定。