C++调用类的方法相同的方法内

问题描述:

I'm写clsLog类:C++调用类的方法相同的方法内

// This is the main DLL file. 

#include "stdafx.h" 

#include <time.h> 

#include <fstream> 
#include <iostream> 
#include <string> 

#include "clsLog.h" 


std::string  m_strCurLogUser; 
int   m_iCurLogAction;   
int   m_iCurLogLevel;  
std::ofstream   m_oCurLogFile; 

// 
// LogInit 
// Initilize the log parameters of the system. 
// The FileName will be the file name that the system will log the messages (LOG.TXT is the default if no names are given). 
// The DatabaseName and TableName specifies the database and table name to be used for log. The default is "LOG_DATABASE" and "LOG_TABLE" 
// will be done. 
// The Action shall be an OR with all the options. Att: If an option is giver (ex: Database) and an invalid name is given, then an error will occur. 
// 
// The default log level is zero (0). So every log with a level upper than that will be logged. A change to the level must be done 
// using the SetLogLevel method. 
// 
// return = 0: Success 
//  1: Failure 
// 
int LogInit (std::string FileName,  // Initialize the log file/connection. 
      std::string DatabaseName, // Database name to connect to. 
      std::string TableName,  // Table name to connect to. 
      std::string UserName,  // User name to log into 
      int Action)    // Action to be done. 
{ 
    // 
    // If the file is already open, someone is calling that function before closing the previous one. Error. 
    // 
    if (m_oCurLogFile.is_open()) 
    { 
     std::string msg; 

     msg = "LogInit called for " + FileName + " witout closing previous session. Error"; 
     this->clsLog::Log (msg); 
    } 

     // Do some stuff 

    return 0; 
} 

// 
// Log 
// Logs the Message according to its Level. 
// 
// return = 0: Success 
//   1: Failure 
// 
int Log (std::string Message, int Level) // Register a log according to the log state. 
{ 
    time_t now; 
    struct tm ptm; 
    char buffer [32]; 

    // 
    // If the sent message level is below the current level, abort. 
    // 
    if (Level < m_iCurLogLevel) 
     return 1; 


    // Get the current date and time and convert it to the time structure (ptm) 
    now = time (NULL); 
    localtime_s (&ptm, &now); 

    // 
    // Format the time structure: DD/MM/AAAA HH:MM:SS) 
    strftime (buffer, 32, "%D/%M/%Y %H:%M:%S", &ptm); 


    // Check if needs to be logged on stdio 
    // 
    if ((m_iCurLogLevel & LOGACTION_STDOUT) == LOGACTION_STDOUT) 
    { 
     cout << buffer + " " + Message; 
    } 

    return 0; 
} 

我可不是能够编译。 I'm收到以下错误在

this->clsLog::Log (msg); 

C2227错误左边的“ - >日志”必须指向类/结构/联合/泛型类型。使用VS2010,Win32应用程序。

帮助赞赏。

的Rds

我没有看到任何证据表明你的函数在类定义中;因此,如错误消息所示,没有“this”指针可用。 “This”仅在类的实例成员中可用。

this指针仅仅是是一个类或结构的一个(非静态)部件的方法中有效。没有this的简单功能,如LogInit

+0

谢谢你的反应。那么应该编码什么? – Cox 2013-04-08 22:49:02

+0

行动。我想我忘了宣布它是一个班级..让我检查... – Cox 2013-04-08 22:50:59

+0

取决于你想要做什么当然。 ;-)我没有clsLog的代码,所以我实际上并不知道你在做什么。你是否尝试删除'this->'?也许'clsLog :: Log()'是一个静态方法,它会工作。否则,请查看'clsLog.h'头文件以了解如何使用它。 – 2013-04-08 22:51:00