2013-04-08 3 views
-1

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 не в состоянии собрать. I'm получаю следующее сообщение об ошибке в

this->clsLog::Log (msg); 

ошибка C2227 на левом «-> Вход» должен указывать на класс/структуры/объединения/общего типа. Использование VS2010, приложение Win32.

Помогите оценить.

Rds

ответ

1

Я не вижу никаких доказательств того, что ваши функции находятся внутри определения класса; поэтому, как говорится в сообщении об ошибке, нет «этого» указателя. «Это» доступно только в членах экземпляра класса.

1

this указатель действителен только внутри метода, который представляет собой (не статический) член класса или структуры. Нет this для простой функции, такой как LogInit.

+0

Спасибо за фальшивый отклик. Что следует кодировать тогда? – Cox

+0

Ops. Я думаю, что забыл объявить его классом. Позвольте мне проверить ... – Cox

+0

Зависит от того, что вы пытаетесь сделать, конечно. ;-) У меня нет кода для clsLog, поэтому я действительно не знаю, что вы делаете. Вы пытались просто удалить 'this->'? Возможно, 'clsLog :: Log()' является статическим методом, и он будет работать. В противном случае перейдите в заголовочный файл 'clsLog.h', чтобы понять, как его использовать. –

Смежные вопросы