C++03 和 C++11 之间的阶级差异
Class differences between C++03 and C++11
我目前正在构建一个应用程序,其中我有一个日志函数,可以在我的大多数类中访问,声明如下:
文件处理程序.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
,你可以告诉编译器你引用的是该类。
相关文章:
- this_thread::sleep_for和计时时钟之间的关系是否由C++11标准指定
- 在 C++11 中利用 int*_t、int_fast*_t 和 int_least*_t 之间的差异的一个很好的例子是
- C++03 和 C++11 之间的c_str功能规范的差异
- C++11 标准中的哪一部分规定了基元数据类型大小之间的相对顺序?
- C 11- ctor()=默认和空ctor(){}之间有什么区别
- C++11 在列表到映射(或其他容器)之间移动元素
- C 11和C 14之间的功能签名差异
- C 11中线程之间的共享文件记录
- C++11 在不同类之间共享固定大小的数组
- 分配列表和初始化器列表之间 C++11 缩小的差异
- C++11 - 2 个向量之间的emplace_back不起作用
- 如何在本地设置和全局设置之间以C 11 RNG的初始状态进行切换
- C++03的throw()说明符和C++11的noexcept之间有什么区别
- 执行未在线程(c++11)之间切换
- C++03 和 C++11 之间的阶级差异
- -lsdc++和-lsdc++11之间的差异
- BOOST_FOREACH 和 c++11 之间基于范围的循环的区别
- C++0x和C++11之间有什么区别
- 在10^10到10^11之间的数字的数据类型应该是什么
- 在运行时可以检测到C++03和C++11之间的差异(如果有的话)