Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ VisualStudio在stdafx.h之前包含一个标头会创建错误_C++_Visual Studio_Stdafx.h - Fatal编程技术网

C++ VisualStudio在stdafx.h之前包含一个标头会创建错误

C++ VisualStudio在stdafx.h之前包含一个标头会创建错误,c++,visual-studio,stdafx.h,C++,Visual Studio,Stdafx.h,让我们假设我有一个班上的人 TestClass.h: #pragma once class Person { int age; std::string name; public: Person(int age, const std::string& name); private: int getAge(); std::string getName(); }; TestClass.cpp #include "stdafx.h" #inclu

让我们假设我有一个班上的人

TestClass.h:

#pragma once

class Person {
    int age;
    std::string name;

public:
    Person(int age, const std::string& name);

private: 
    int getAge();
    std::string getName();
};
TestClass.cpp

#include "stdafx.h"
#include "TestClass.h"

Person::Person(int age, const std::string& name) : age(age), name(name) {};

int Person::getAge() {
    return age;
}

std::string Person::getName() {
    return name;
}
对于本例,代码进行编译。 但是,如果我在TestClass.cpp中的第1行和第2行之间切换并包含 其次,由于某种原因,我得到了以下编译错误:

'Person': is not a class or namespace name  
missing type specifier - int assumed. Note: C++ does not support default-int    
'Person': constructor initializer lists are only allowed on constructor definitions 
'Person': is not a class or namespace name  
'Person': function should return a value; 'void' return type assumed    
'age': undeclared identifier    
'Person': is not a class or namespace name  
'name': undeclared identifier
很明显,如果我先包含stdafx.h,我不会有任何问题,但我似乎不明白为什么会发生这种情况。任何帮助都将不胜感激。

请执行以下操作:

包含到TestClass.h中 禁用预编译头 然后它应该可以工作。

执行以下操作:

包含到TestClass.h中 禁用预编译头

那么它应该可以工作。

stdafx.h是一个奇怪的特定于Visual Studio的文件,具有特殊的含义和行为:如果它坚持先做,就让它做吧。VisualStudio不会编译stdafx.h之前的文件中的任何内容。因为他们这么说。@user4581301这就是我要找的。我知道stdafx.h是一个特定于VS的文件,我认为这可能是故意的,但我找不到任何明确表示在文件包含之前不能出现任何内容的内容。谢谢。由于您缺乏一个来自马口的解释。我能做的最权威的回答就是上一个链接。如果您不需要stdafx.h,最好在创建新项目时选择空项目。这样,标准C++文件将正常编译而不包括STDAFX。h@LưuVĩnhPhúc我知道如何让代码编译,问题的关键是这样我才能理解为什么如果stdafx.h不在第一行,代码就不会编译。这是假设stdafx.h是必需的。stdafx.h是一个奇怪的特定于VisualStudio的文件,具有特殊的含义和行为:如果它坚持先做,就让它做吧。VisualStudio不会编译stdafx.h之前的文件中的任何内容。因为他们这么说。@user4581301这就是我要找的。我知道stdafx.h是一个特定于VS的文件,我认为这可能是故意的,但我找不到任何明确表示在文件包含之前不能出现任何内容的内容。谢谢。由于您缺乏一个来自马口的解释。我能做的最权威的回答就是上一个链接。如果您不需要stdafx.h,最好在创建新项目时选择空项目。这样,标准C++文件将正常编译而不包括STDAFX。h@LưuVĩnhPhúc我知道如何让代码编译,问题的关键是这样我才能理解为什么如果stdafx.h不在第一行,代码就不会编译。这是假设stdafx.h是必需的。正如我在评论和实际问题中提到的,我知道如何编译代码。问题不在于如何让代码编译,而在于如果stdafx.h不在第一行,为什么代码不编译。我试过你的代码,经过这些修改,它编译得很好。即使stdafx.h包含在TestClass.h之后,您使用什么来编译代码?我使用VS 2015,但它没有编译。如果您使用的是VS,那么由于VS不会编译stdafx.h之前的任何内容,因此编译的代码就没有意义了,除非编译选项/Yu'stdafx.h'被取消选中,正如我在使用VS2013的回答中提到的那样/“未检查”表示“禁用预编译头”。这个建议是针对像我这样懒惰的程序员的。正确的解决方案是重新创建.pch文件,例如在更改包含文件的顺序后将其删除。在重新创建.pch文件之后,它将很好地编译。正如我在一篇评论和实际问题中提到的,我知道如何让代码编译。问题不在于如何让代码编译,而在于如果stdafx.h不在第一行,为什么代码不编译。我试过你的代码,经过这些修改,它编译得很好。即使stdafx.h包含在TestClass.h之后,您使用什么来编译代码?我使用VS 2015,但它没有编译。如果您使用的是VS,那么由于VS不会编译stdafx.h之前的任何内容,因此编译的代码就没有意义了,除非编译选项/Yu'stdafx.h'被取消选中,正如我在使用VS2013的回答中提到的那样/“未检查”表示“禁用预编译头”。这个建议是针对像我这样懒惰的程序员的。正确的解决方案是重新创建.pch文件,例如在更改包含文件的顺序后将其删除。在重新创建.pch文件后,它将很好地编译。