0

我不断收到 cpp 错误,询问我是否忘记包含我在标题中完成的 stdafx.h,错误代码是 C1010。

完整的错误为:在寻找预编译头文件时意外结束文件。您是否忘记将 '#include "stdafx.h"' 添加到您的源代码中?

首先,我有一个头文件,它定义了计算器的一些基本功能。调用时接受参数。

#pragma once
#include <iostream>
#include <string>
#include "stdafx.h"

using namespace std;

class Functions
{
public:
    Functions() {};

    float add(float a, float b);
    float subtract(float a, float b);
    float multiply(float a, float b);
    float divide(float a, float b);

private:
    float answer;
};

然后是 cpp,它简单地计算 2 个参数并返回答案。

#pragma once
#include "Functions.h"

float Functions::add(float a, float b)
{
    answer = a + b;
    return answer;
}

float Functions::subtract(float a, float b)
{
    answer = a - b;
    return answer;
}

float Functions::multiply(float a, float b)
{
    answer = a * b;
    return answer;
}

float Functions::divide(float a, float b)
{
    answer = a / b;
    return answer;
}

请简单解释一下,我不是很擅长编码。

4

1 回答 1

0

stdafx.h 是 Visual Studio 使用的预编译头文件,您可以将其删除。

编辑:事实证明,这仅在您关闭 Visual Studio 中的预编译头文件时才有效。默认情况下,它们在 Visual Studio 中处于启用状态。

如果您想保留它们,它们必须在任何其他include之前。

所以你的预处理器指令应该是:

#include "stdafx.h"
#include <iostream>
#include <string>
于 2020-02-06T14:52:20.250 回答