【问题标题】:Is there something wrong with how I've created this class?我创建这个类的方式有问题吗?
【发布时间】:2020-02-06 14:45:45
【问题描述】:

我不断收到 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;
}

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

【问题讨论】:

  • using 放在头文件中是非常糟糕的做法
  • 不要将 #pragma once 添加到 cpp 文件中,它不会做任何有用的事情

标签: class stdafx.h


【解决方案1】:

stdafx.h是visual studio使用的预编译头文件,可以去掉。

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

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

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

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

【讨论】:

  • 我删除了 stdafx.h 的包含,仍然得到同样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 2014-08-02
相关资源
最近更新 更多