我已经阅读了很多关于此事的问题,但似乎没有一个问题可以解决我的问题。代码如下:
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#开发人员的角度来看,这段代码没问题,但我不知道,我还是初学者。
答案 0 :(得分:1)
有几件事你做得很奇怪。但最重要的是你不需要在.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();
};
}