C++ 结构C+之前需要初始化器+;
在第一行中,我得到一个错误,声明“在结构之前预期initilizer”。我对程序中的错误感到困惑 <>我从UDMY学习到这一点,我的平均数是C++,如果有人知道有什么问题,请告诉我。<强>谢谢< <强>p>C++ 结构C+之前需要初始化器+;,c++,struct,C++,Struct,在第一行中,我得到一个错误,声明“在结构之前预期initilizer”。我对程序中的错误感到困惑 我从UDMY学习到这一点,我的平均数是C++,如果有人知道有什么问题,请告诉我。谢谢< p> #ifndef HELPER_H #define HELPER_H #include <ctime> #include <string> #include <sstream> #include <fstream> namespace Helper {
#ifndef HELPER_H
#define HELPER_H
#include <ctime>
#include <string>
#include <sstream>
#include <fstream>
namespace Helper
{
template <class T>
std::string ToString(const T &);
struct DateTime
{
DateTime()
{
time_t ms;
time (&ms);
struct tm *info = localtime(&ms);
D = info->tm_mday;
m = info->tm_mon + 1;
y = 1900 + info->tm_year;
M = info->tm_min;
H = info->tm_hour;
S = info->tm_sec;
}
DateTime(int D,int m,int y,int M,int H,int S):D(D),m(m),y(y),H(H),M(M),S(S) {}
DateTime(int D,int m,int y) : D(D) , y(y) , m(m) , H(0) , M(0) , S(0) {}
DateTime Now() const
{
return DateTime();
}
int D,m,y,H,M,S;
std::string GetDateString() const
{
return std::string(D < 10 ? "0" : "") + ToString(D) +
std::string(m <10 ? ".0" : ".") + ToString(m) + "." +
ToString(y);
}
std::string GetTimeString(const std::string &sep = ":") const
{
return std::string(H< 10 ? "0" : "") + ToString(H) + sep +
std::string(M< 10 ? "0" : "") + ToString(M) + sep +
std::string(S<10 ? sep : "") + ToString(S);
}
std::string GetDateTimeString(const std::string &sep = ":") const
{
return GetDateString() + " " + GetTimeString(sep);
}
};
template<class T>
std::string ToString(const T &e)
{
std::ostringstream s;
s << e;
return s.str();
}
void WriteAppLog(const std::string &s)
{
std::ofstream file("AppLog.txt", std::ios::app);
file << "[" << Helper::DateTime.GetDateTimeString() << "[" << "\n" << s << std::endl << "\n";
file.close();
}
}
#endif // HELPER_H
F:\Work-related\Keylogger\Helper.h:34:10:警告:在此处初始化时[-Wreorder]
^
日期时间(intd,intm,inty):D(D),y(y),m(m),H(0),m(0),S(0){
结构定义前缺少分号。它是:
std::string ToString(const T &)
struct DateTime
应该是:
std::string ToString(const T &);
struct DateTime
没关系,刚才看到了你的代码编辑 我想问题在于您没有在函数
WriteAppLog
中声明对象。尝试将其更改为:
void WriteAppLog(const std::string &s)
{
Helper::DateTime dateTime;
std::ofstream file("AppLog.txt", std::ios::app);
file << "[" << dateTime.GetDateTimeString() << "[" << "\n" << s << std::endl << "\n";
file.close();
}
void WriteAppLog(const std::string&s)
{
Helper::DateTime DateTime;
std::of流文件(“AppLog.txt”,std::ios::app);
函数deparation.Nevermind之后缺少一个;文件。在结构之前缺少一个分号。我用分号关闭了结构函数,但仍然有相同的错误,无法上载结尾,因为我的代码多于word:(你能正确地发布你的代码吗?有很多拼写错误和范围问题。你没有做建议的更改。从建议开始。我做了更改,现在有3个不同的警告。你能发布错误消息吗?它们是因为其他原因,尽管这些只是关于构造函数初始化我的值的警告n与声明的顺序不同。似乎你可以忽略它们。进行谷歌搜索或打开另一个问题。多谢各位,我会搜索的。