Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.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++ 使用C++;Visual Studio中的类-未声明的标识符错误_C++_Visual Studio 2015 - Fatal编程技术网

C++ 使用C++;Visual Studio中的类-未声明的标识符错误

C++ 使用C++;Visual Studio中的类-未声明的标识符错误,c++,visual-studio-2015,C++,Visual Studio 2015,我希望这是难以置信的简单,但我不能为我的生活找出什么是错的。我是C++新手,我在VisualStudio中创建了C++类,我试图在另一个文件的主方法中使用它。我已经把所有的东西都减到最低限度了,但是我还是不能让它运行。我得到的第一个编译错误是“Test”:未声明的标识符。如果我删除“Test;”从App.cpp中,所有的编译都很好。 下面是代码。有人能帮忙吗 App.cpp: #include "Test.h" #include "stdafx.h" #include <iostream&

我希望这是难以置信的简单,但我不能为我的生活找出什么是错的。我是C++新手,我在VisualStudio中创建了C++类,我试图在另一个文件的主方法中使用它。我已经把所有的东西都减到最低限度了,但是我还是不能让它运行。我得到的第一个编译错误是“Test”:未声明的标识符。如果我删除“Test;”从App.cpp中,所有的编译都很好。 下面是代码。有人能帮忙吗

App.cpp:

#include "Test.h"
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    Test test;
    //cout << test.getNumber() << endl;
    return 0;
}
Test.cpp:

#include "stdafx.h"
#include "Test.h"


int Test::getNumber()
{
    return number;
}

Test::Test()
{
    this->number = 1;
}


Test::~Test()
{
}
问题是
“stdafx.h”
是预编译的。 VisualC++在源代码文件中不编译任何代码之前,包含“StdAFx.h”< /C>,除非编译选项<代码> /Yu'StdAFx.h '/Cuth]未被选中(默认情况下);它假定在该行之前(包括该行)源代码中的所有代码都已编译

解决方案是将其移动到第一个
include


您可以在的wiki页面上阅读更多信息

此包含“β”的“测试”h包括“StdAFX.h”VisualStudio在编译之前不编译任何东西,如果使用预编译头,则代码“>”包含“STDAFX.H”<代码>。问题是VS默认使用VisualC++中的“预编译头”特征,这使得预处理器行为明显非标准方式。正如@VladfromMoscow所指出的,一种修复方法是让预编译头(这里
“stdafx.h”
)成为每个实现文件中的第一个包含项。更好的解决方案是在项目设置中关闭预编译头。右键单击项目→ 性质→ 把它关掉。好了,真的吗?就这样。。。我说过一定很简单。谢谢
#include "stdafx.h"
#include "Test.h"


int Test::getNumber()
{
    return number;
}

Test::Test()
{
    this->number = 1;
}


Test::~Test()
{
}