- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当它在 main.cpp 中时它可以工作,但是如果我放入 log.cpp 中,当我使用任意数量的参数调用日志函数时会出现 LNK2019 错误。
string getDate()
{
SYSTEMTIME localTime;
TCHAR strTime[128];
GetLocalTime(&localTime);
wsprintf(strTime, "%04d_%02d_%02d_", localTime.wYear, localTime.wMonth, localTime.wDay);
stringstream strm;
strm << strTime;
string date = strm.str();
return &date[0];
}
string getHour()
{
SYSTEMTIME localTime;
TCHAR strTime[128];
GetLocalTime(&localTime);
wsprintf(strTime, "%02d:%02d:%02d >> ", localTime.wHour, localTime.wMinute, localTime.wSecond);
stringstream strm;
strm << strTime;
string hour = strm.str();
return &hour[0];
}
void addToStream(stringstream& /*a_stream*/)
{
}
template<typename T, typename... Args>
void addToStream(stringstream& a_stream, const T& a_value, Args... a_args)
{
a_stream << a_value;
addToStream(a_stream, a_args...);
}
template<typename... Args>
void log(Args... a_args)
{
stringstream strm;
addToStream(strm, a_args...);
string s = strm.str();
HKEY key;
char appPath[1024];
DWORD appPathLength = 1024;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Session Manager\\Environment", 0, KEY_READ | KEY_WRITE, &key) != ERROR_SUCCESS)
{
RegCloseKey(key);
}
else
{
if (RegQueryValueEx(key, "SERVIDOR_DIR", NULL, NULL, (LPBYTE)&appPath, &appPathLength) != ERROR_FILE_NOT_FOUND)
{
HANDLE hFile;
string dateProv = getDate();
char *date = new char[dateProv.size() + 1];
copy(dateProv.begin(), dateProv.end(), date);
date[dateProv.size()] = '\0';
char fileNamePath[MAX_PATH];
strcpy(fileNamePath, appPath);
strcat(fileNamePath, "\\logs\\painel\\");
strcat(fileNamePath, date);
strcat(fileNamePath, "levaetraz.log");
puts(fileNamePath);
hFile = CreateFile(
fileNamePath, // file to open
GENERIC_WRITE, // open for writing
0, // share for writing
NULL, // default security
// CREATE_NEW, // existing file only
//CREATE_ALWAYS, // creates a new file, always.
OPEN_ALWAYS, // creates a new file, always.
FILE_ATTRIBUTE_NORMAL, // normal file
NULL // no attr. template
);
if (hFile != INVALID_HANDLE_VALUE)
{
// Write to File
BOOL bErrorFlag = FALSE;
string hourProv = getHour();
char *hour = new char[hourProv.size() + 1];
copy(hourProv.begin(), hourProv.end(), hour);
hour[hourProv.size()] = '\0';
char stringLog[1024];
strcpy(stringLog, hour);
strcat(stringLog, &s[0]);
strcat(stringLog, "\r\n");
puts(stringLog);
DWORD dwPtr = SetFilePointer(hFile, 0, NULL, FILE_END); //set pointer position to end file
DWORD dwBytesToWrite = lstrlen(stringLog);
DWORD a = 0;
bErrorFlag = WriteFile(
hFile, // open file handle
stringLog, // start of data to write
dwBytesToWrite, // number of bytes to write
&dwPtr, // number of bytes that were written
NULL // no overlapped structure
);
delete[] hour;
}
delete[] date;
CloseHandle(hFile);
RegCloseKey(key);
}
RegCloseKey(key);
}
}
在我提出可变数量的参数和调用 log("any thing") 的可能性之前,那么这将如何在具有可变数量的参数的 log.cpp 中工作?
void log(char *log)
{
HANDLE hFile;
std::string dateProv = getDate();
char *date = new char[dateProv.size() + 1];
std::copy(dateProv.begin(), dateProv.end(), date);
date[dateProv.size()] = '\0';
char fileNamePath[MAX_PATH];
strcpy(fileNamePath, "logs/painel/");
strcat(fileNamePath, date);
strcat(fileNamePath, "levaetraz.log");
puts(fileNamePath);
hFile = CreateFile(
fileNamePath, // file to open
GENERIC_WRITE, // open for writing
0, // share for writing
NULL, // default security
// CREATE_NEW, // existing file only
//CREATE_ALWAYS, // creates a new file, always.
OPEN_ALWAYS, // creates a new file, always.
FILE_ATTRIBUTE_NORMAL, // normal file
NULL // no attr. template
);
if (hFile != INVALID_HANDLE_VALUE)
{
// Write to File
BOOL bErrorFlag = FALSE;
std::string hourProv = getHour();
char *hour = new char[hourProv.size() + 1];
std::copy(hourProv.begin(), hourProv.end(), hour);
hour[hourProv.size()] = '\0';
char stringLog[1024];
strcpy(stringLog, hour);
strcat(stringLog, log);
strcat(stringLog, "\r\n");
puts(stringLog);
DWORD dwPtr = SetFilePointer(hFile, 0, NULL, FILE_END); //set pointer position to end file
DWORD dwBytesToWrite = lstrlen(stringLog);
DWORD a = 0;
bErrorFlag = WriteFile(
hFile, // open file handle
stringLog, // start of data to write
dwBytesToWrite, // number of bytes to write
&dwPtr, // number of bytes that were written
NULL // no overlapped structure
);
delete[] hour;
}
delete[] date;
CloseHandle(hFile);
}
我创建了一个 log.h 并包含在 main.cpp 中
#pragma once
template<typename... Args>
void log(Args... a_args);
缺少什么?
最佳答案
使用当前的编译器,除了具有已知类型/值的实例化之外,您无法单独编译模板代码。
主要问题似乎是这段代码:
template<typename T, typename... Args>
void addToStream(stringstream& a_stream, const T& a_value, Args... a_args)
{
a_stream << a_value;
addToStream(a_stream, a_args...);
}
template<typename... Args>
void log(Args... a_args)
{
stringstream strm;
addToStream(strm, a_args...);
string s = strm.str();
// More stuff
我建议您保留标题中的第一个函数模板,但将第二个替换为
template<typename... Args>
void log(Args... a_args)
{
stringstream strm;
addToStream(strm, a_args...);
logString( strm.str() );
}
然后您可以单独编译新函数 logString
,因为它不是模板化的。
关于C++ Win32 如何将此函数放在单独的 cpp 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30446705/
我当前项目中的许多类都有几个只能从类本身内部调用的属性和方法。此外,根据类(class)的当前状态,它们可能会扰乱类(class)的工作。 目前,所有这些接口(interface)都在 .h 文件的主
我想使用ngbTabSet将nav-pills in card-header 和tab-content in card-body。但我不知道该怎么做。 这是我尝试实现的示例(使用 bootstrap.
我正在浏览文档以查找如何允许放置在停靠栏图标上。据我所知,建议您使用 LSItemContentTypes,因为 CFBundleTypeOSTypes 已弃用。但是,我无法让 LSItemConte
我正在尝试在书签中使用 jquery UI 作为 slider 。并且 jquery ui 要求在普通 jquery 文件之后包含该文件。 所以到目前为止我所尝试的只是将脚本附加到 header ,同
您好,我尝试了广泛的谷歌搜索,但似乎没有任何帮助。 这是我的场景: 公司 Logo 存储在外部域/网址(矩形)中。 带有谷歌地图的 Ionic 应用程序,将这些 Logo 作为标记放置在 map 上。
我今天在阅读我的一些 C# 代码时发现了这一行: if (ProgenyList.ItemContainerGenerator.Status != System.Windows.Controls.Pr
我刚遇到this question在 Go FAQ 中,它让我想起了困扰我一段时间的事情。不幸的是,我真的不明白答案是什么。 似乎几乎所有非 C 类语言都将类型放在变量名之后,如下所示: var :
这是有效的 HTML 吗? 最佳答案 作为元素可以包含 phrasing content ,以及属于该组,则有效。 从语义上(并且具有一些常识),如果唯一的内容是 , 不;如果它同时包含文本和图像
这是有效的 HTML 吗? 最佳答案 作为元素可以包含 phrasing content ,以及属于该组,则有效。 从语义上(并且具有一些常识),如果唯一的内容是 , 不;如果它同时包含文本和图像
我有两本 T.Parr 写的关于 ANTLR 的书,我到处都看到美元符号和符号的引用。它也对我有用: term : IDENT -> { new TokenNode($IDENT) }; 或更复杂的东
我在实现段控制时遇到了一些问题。因为我希望它是一个固定的标题,所以当我滚动时我总是可以看到它,我已经在 - (UIView *)tableView:(UITableView *)tableView v
我有一个 20x36px (10x18pt) 的箭头图像,当我选择一个 UIImageView 时,将它拖到我的 View Controller 上然后设置图像,它总是模糊的。我只在我的项目中包含 @
How can I put background image when I hover a link Insert Bg in this a when hov
我需要在我的 .container 中添加(最新的第一个)新分区,但在 .controls (按钮)之后。可能吗? 注意:添加新的分区来保存按钮下方的前置控件对我来说不是一个选择。 HTML 需要保持
我是一名优秀的程序员,十分优秀!