在Visual Studio中使用C ++类 - 未声明的标识符错误[重复]

问题描述 投票:2回答:1

我希望这非常简单,但不能为我的生活弄清楚什么是错的。我是C ++的新手,我在visual studio中创建了一个C ++类,并尝试在另一个文件的main方法中使用它。我把所有东西都剥离到了最低限度,但我仍然无法让它运行。我得到的第一个编译错误是'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.h:

#pragma once
class Test
{
private:
    int number;

public:
    int getNumber();
    Test();
    ~Test();
};

TEST.CPP:

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


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

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


Test::~Test()
{
}
c++ visual-studio-2015
1个回答
0
投票

问题是"stdafx.h"是预编译的。 Visual c ++不会在源文件中的#include "stdafx.h"之前编译任何内容,除非取消选中编译选项/Yu'stdafx.h'(默认情况下);它假设源中包含该行的所有代码都已编译。

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

你可以在Precompiled header的wiki页面上阅读更多相关信息。

© www.soinside.com 2019 - 2024. All rights reserved.