"演示项目::记录器" : "类"类型重新定义

'DemoProject::Logger' : 'class' type redefinition

本文关键字:新定义 定义 项目 记录器 类型      更新时间:2023-10-16

我读了很多关于这件事的问题,但似乎没有一个能解决我的问题。下列代码:

Logger.cpp

#include "Includes.h"
namespace DemoProject {
    class Logger {
    public:
        static void Logger::printm(CEGUI::String Message) {
            std::cout << currentDateTime() << " >> " << Message << std::endl;
        }
    private:
        static const std::string currentDateTime() {
            time_t     now = time(0);
            struct tm  tstruct;
            char       buf[80];
            tstruct = *localtime(&now);
            strftime(buf, sizeof(buf), "%d-%m-%Y %X", &tstruct);
            return buf;
        }
    };
}

logger.h

#ifndef LOGGER_H
#define LOGGER_H
#pragma once
#include "Includes.h"
namespace DemoProject {
    class Logger {
    public:
        static void Logger::printm(CEGUI::String Message);
    };
}
#endif

Includes.h

#ifndef INCLUDES_H
#define INCLUDES_H
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <CEGUI/CEGUI.h>
#include <CEGUI/RendererModules/OpenGL/GLRenderer.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include "Logger.h"
#endif

很抱歉这篇文章格式不好,但这是我能做的最好的了。我主要是一名c#开发人员,但我正在尝试通过自己创建的不同练习来学习c++。从c#开发人员的角度来看,这段代码是可以的,但我不知道,我还是一个初学者。

你有几件事做得很奇怪。但最重要的是,您不需要在.cpp文件中再次声明该类。您只需实现以下函数:

namespace DemoProject {
    void Logger::printm(CEGUI::String Message) {
        std::cout << currentDateTime() << " >> " << Message << std::endl;
    }
    static const std::string currentDateTime() {
        ...
    }
}

你也没有在头文件中声明currentDateTime,所以不能正确编译。你也不需要在声明中定义类的作用域,因为你已经在这个类中了,所以你的头应该是这样的:

namespace DemoProject {
    class Logger {
    public:
        static void printm(CEGUI::String Message);
        static const std::string currentDateTime();
    };
}