简体   繁体   中英

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

#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

#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. I was then receiving an error on "log logHandler" saying log is not a declared name.

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?

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.

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

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.

By explicitly stating class log , you tell the compiler that you are referring to the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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