简体   繁体   English

C ++ 03和C ++ 11之间的类差异

[英]Class differences between C++03 and C++11

I'm current building an application in which I have a log function that is accessible in most of my classes which was declared as below: 我当前正在构建一个应用程序,其中有一个日志函数,该函数在我的大多数类中都可以访问,声明如下:

FileHandler.h FileHandler.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

Log.h Log.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

This worked fine for a long time and then I wanted to include another function elsewhere in my application that was only compatible with C++11 so I told the compiler to compile to these standards. 很长时间以来,这种方法都运行良好,然后我想在应用程序中的其他位置包含仅与C ++ 11兼容的另一个函数,因此我告诉编译器按照这些标准进行编译。 I was then receiving an error on "log logHandler" saying log is not a declared name. 然后,我在“ log logHandler”上收到错误消息,说log不是声明的名称。

I was able to resolve the problem by changing the line to 通过将行更改为

class log logHandler;

I was wondering if anybody could tell me what has changed between C++03 and C++11 that required me to do this? 我想知道是否有人可以告诉我在C ++ 03和C ++ 11之间发生了什么变化,我需要这样做吗?

EDIT: Included all relevant code to make question more complete. 编辑:包括所有相关代码以使问题更加完整。

You don't show your real code (missing ; at the end of the class declaration, no #endif ), but chances are that your problem is somehow related to std::log , which has received a new overload in C++11, in combination with a using namespace std somewhere in your code. 您没有显示真实的代码(缺少;在类声明的末尾,没有#endif ),但是您的问题有可能与std::log ,后者在C ++ 11中收到了新的重载,与代码中某处的using namespace std结合using namespace std

Note that the new overload is probably irrelevant to the problem at hand; 请注意,新的过载可能与手头的问题无关; the real reason may very well be a change somewhere in your compiler's standard-library implementation causing an internal #include <cmath> . 真正的原因很可能是编译器的标准库实现中的某个更改导致内部#include <cmath> This means that even in C++03, your code was only working by sheer coincidence, and a conforming C++03 compiler was always allowed to reject it. 这意味着即使在C ++ 03中,您的代码也只是巧合地工作,并且始终允许符合标准的C ++ 03编译器拒绝它。

Here is an example program which may reproduce your problem: 这是一个示例程序,可能会重现您的问题:

#include <cmath>

using namespace std;

struct log
{
};

int main()
{
    // log l; // does not compile
    struct log l; // compiles
}

Nothing has changed about how the code you posted is treated. 您对待发布的代码的方式没有任何改变。

What I suspect is, that you somewhere have an 我怀疑的是,您某处有一个

#include <cmath>

And below that, somewhere else 然后在其他地方

using namespace std;

This causes your compiler to not be able to unambiguously resolve the name log , since there is std::log (a function) and your class log. 这将导致你的编译器不能够明确地解析名称log ,因为没有std::log (函数)和类日志。

By explicitly stating class log , you tell the compiler that you are referring to the class. 通过显式说明class log ,可以告诉编译器您正在引用该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM