我看了很多关于这个问题的问题,但似乎没有一个能解决我的问题。代码如下:
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/