1
當這是在main.cpp它的工作,但如果我把log.cpp得到LNK2019錯誤,當我用任何數量的參數調用日誌功能。C++ Win32如何將這個函數放在單獨的cpp文件上?
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.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);
缺少什麼?
你能發表完整的鏈接錯誤嗎? – RedAgito
等等,.. log是模板化的?你必須包含它的源頭 – RedAgito
可能的重複[什麼是未定義的引用/未解析的外部符號錯誤,我該如何解決它?](http://stackoverflow.com/questions/12573816/what-is-an -undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – IInspectable