C++03 和 C++11 之间的阶级差异

Class differences between C++03 and C++11

本文关键字:C++11 之间 C++03      更新时间:2023-10-16

我目前正在构建一个应用程序,其中我有一个日志函数,可以在我的大多数类中访问,声明如下:

文件处理程序.h

#ifndef FILEHANDLER_H
#define FILEHANDLER_H
#pragma once
#include <SDL.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cctype>
//Include to allow logging
#include "log.h"
class fileHandler
{
    public:
        fileHandler();
        virtual ~fileHandler();
        void WriteToFile(const std::string& filename, std::string textToWrite);
        std::vector<std::string> ReadFromFile(const std::string& filename);
        std::string& TrimString(std::string& stringToTrim);

    protected:
    private:
        class log logHandler;
        std::vector<std::string> blockOfText;
        std::string currentLine;
};
#endif // FILEHANDLER_H

日志.h

#ifndef LOG_H
#define LOG_H
#pragma once
#include <SDL.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <time.h>
class log
{
    public:
        log();
        virtual ~log();
        void static WriteToConsole(std::string textToWrite);
        void WriteToLogFile(std::string textToWrite);
    protected:
    private:
};
#endif // LOG_H

这在很长一段时间内都工作得很好,然后我想在我的应用程序中的其他地方包含另一个仅与 C++11 兼容的函数,所以我告诉编译器编译到这些标准。然后我在"日志处理程序"上收到一个错误,说日志不是声明的名称。

我能够通过将行更改为

class log logHandler;

我想知道是否有人可以告诉我 C++03 和 C++11 之间发生了什么变化需要我这样做?

编辑:包括所有相关代码以使问题更完整。

您没有显示您的真实代码(在类声明的末尾缺少;,没有#endif(,但您的问题很可能与 std::log 有关,它在 C++11 中收到了新的重载,并结合了代码中某处的using namespace std

请注意,新的重载可能与手头的问题无关;真正的原因很可能是编译器的标准库实现中的某个更改导致内部#include <cmath>。这意味着即使在 C++03 中,您的代码也只是纯粹的巧合,并且始终允许符合 C++03 编译器拒绝它。

这是一个示例程序,可能会重现您的问题:

#include <cmath>
using namespace std;
struct log
{
};
int main()
{
    // log l; // does not compile
    struct log l; // compiles
}

您发布的代码的处理方式没有任何变化。

我怀疑的是,你在某个地方有一个

#include <cmath>

在那之下,其他地方

using namespace std;

这会导致编译器无法明确解析名称log,因为存在std::log(函数(和类日志。

通过显式声明class log,你可以告诉编译器你引用的是该类。