c++ - 'DemoProject::Logger' : 'class' 类型重定义

标签 c++

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

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() {
        ...
    }

}

您也没有在 header 中声明 currentDateTime,因此无法正确编译。你也不需要在声明中限定类的范围,因为你已经在类中了,所以你的标题应该如下所示:

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

关于c++ - 'DemoProject::Logger' : 'class' 类型重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32750532/

相关文章:

c++ - 如何使用 Windows 操作系统在 C++ 中获取 UTC 时间偏移

c++ - Helper 类具有隐藏的 COM 依赖项;谁调用 CoInitialize?

c++ - 连接组件计数

c++ - 使用代码将字符集设置为多字节

c++ - 如何在 C++ 中通过相交或不相交对子 vector 进行分组

c++ - 我丢失的 Edge 在哪里?

c++ - 使用 objective-c 文件中的 c++ 模板类

c++ - 返回指向函数中声明的数据的指针

python - 在 HDF5 C++ api 中使用 GZIP 压缩时,是否默认启用自动分块?

c++ - 了解 Scott Meyers 的第三个 std::weak_ptr 示例