C++ #查找预编译头时跳过包含使用--查找预编译头时意外结束文件

C++ #查找预编译头时跳过包含使用--查找预编译头时意外结束文件,c++,visual-c++,C++,Visual C++,我有一些代码,但无法编译。我无法进一步介绍这个示例,因为每次尝试调试时都会出现此错误。 注意:我是Java程序员,VS中的这个消息很奇怪,我找不到问题所在。有人能解释一下这个错误是什么意思吗 以下是代码: zadatak.cpp: // zadatak.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "String.h" #include <iostrea

我有一些代码,但无法编译。我无法进一步介绍这个示例,因为每次尝试调试时都会出现此错误。 注意:我是Java程序员,VS中的这个消息很奇怪,我找不到问题所在。有人能解释一下这个错误是什么意思吗

以下是代码:

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;
}
//zadatak.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括“String.h”
#包括
使用名称空间std;
void main()
{
串s1(20)、s2(20);
cout
1> 生成失败。
1>
1> 时间流逝00:00:00.41
======生成:0成功,1失败,0最新,0跳过==========

你想要

#include "stdafx.h"

在String.cpp文件中,与所有.cpp文件一样。

您使用默认的MSVC项目,其中包括。我建议您在创建项目时选择“不使用预编译头选项”。

您应该添加

#include "stdafx.h"
在String.cpp文件中,但如果使用预编译头,则必须将其放在顶部,例如:

#include "stdafx.h"
#include "String.h"
#include <iostream>
using namespace std;
#include <string.h>
#包括“stdafx.h”
#包括“String.h”
#包括
使用名称空间std;
#包括
关于预编译头必须放在第一位的原因进行了广泛的讨论


编译器会忽略PCH之前的所有内容,因此PCH必须放在第一位。VS 2013>Alt+F7>配置属性>C/C++>所有选项>SDL检查中的清除选项>应用>确定。

我用GCC尝试了这个方法,它编译得很好,我只是更改了string.h文件,添加了、#ifndef string_h、#define string_h、#endif可能重复的。对于我的特定情况,我只需将有问题的头文件移到include的末尾。
#include "stdafx.h"
#include "stdafx.h"
#include "String.h"
#include <iostream>
using namespace std;
#include <string.h>