无法解析的外部符号 "

举个栗子demo 2015-05-18 10:44:47
控制台程序,main()中定义了一个log文件记录实例:
CLogFile m_logfile;
CLogFile 类是从一个MFC程序移植过来的,
调用CLogFile 的WriteLog(CString LogText),编译无错,连接错误

大神们帮小弟看看

error LNK2001: 无法解析的外部符号 "public: static bool __cdecl CLogFile::WriteLog(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > >)" (?WriteLog@CLogFile@@SA_NV?$CStringT@_WV?$StrTraitATL@_WV?$ChTraitsCRT@_W@ATL@@@ATL@@@ATL@@@Z)

函数如下:
bool CLogFile::WriteLog(CString LogText)
{
try
{
// CFile m_File;
CStdioFile m_SFile;
CFileFind m_FileFind;
CString m_sErrorMessage;
CString m_sFileName = GetFileName();
CString m_sFilePath = GetFilePath();
CString m_sCurrentTime = CTime::GetCurrentTime().Format(L"%Y-%m-%d %X");
m_sErrorMessage = m_sCurrentTime + L" : ";
m_sErrorMessage += LogText + L"\r";
if(!m_FileFind.FindFile(m_sFilePath))
{
CreateDirectory(m_sFilePath,NULL);
}
if(!m_SFile.Open(m_sFilePath + "\\" +m_sFileName,CFile::modeReadWrite))
{
m_SFile.Open(m_sFilePath + "\\" + m_sFileName,CFile::modeCreate | CFile::modeReadWrite | CFile::typeText);
}
m_SFile.SeekToEnd();
m_SFile.Write(m_sErrorMessage,m_sErrorMessage.GetLength()*2);
m_SFile.Close();
}
catch(CFileException fileException)
{
return false;
}

return true;
}
...全文
522 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lx624909677 2015-06-13
  • 打赏
  • 举报
回复
引用 5 楼 u011673029 的回复:
cstring不是mfc里的吗?你用string试试
只要包含CString类对应的头文件,就可以使用这个类
jixiang1983 2015-06-13
  • 打赏
  • 举报
回复
前面加上 #include <afx.h> 试试,控制台应用程序使用CString 必须包含相应头文件
赵4老师 2015-06-12
  • 打赏
  • 举报
回复
尺有所短,寸有所长:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt||0==pszFmt[0]) return;
    _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
        } else {
            fclose(flog);
        }
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
#endif
    for (i=0;i<10000;i++) {
        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
    }
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中
自在飞花6541 2015-06-12
  • 打赏
  • 举报
回复
cstring不是mfc里的吗?你用string试试
oyljerry 2015-05-19
  • 打赏
  • 举报
回复
函数对应的cpp是否包含进工程编译,同时你的工程要支持MFC
mlqxj35674 2015-05-19
  • 打赏
  • 举报
回复
控制台程序有没有使用MFC啊s
用户 昵称 2015-05-19
  • 打赏
  • 举报
回复
参数不对。
worldy 2015-05-18
  • 打赏
  • 举报
回复
调用位置的参数类型和函数定义的参数类型不一致

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧