2

我对编程很陌生,但在过去一周左右一直在关注 c++ 教程并积累了许多 PDF 来帮助我。我在他们或网上找不到任何足够清楚地回答我的问题的东西。请原谅我的新手。

相关代码:

日志文件.hpp

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// HEADER :: CLASS INTERFACE FILE

// ==============================
// Include Guard
#ifndef __LOGFILE_INCLUDED__ // If Actor.hpp hasn't been included yet
#define __LOGFILE_INCLUDED__ // Define this for the compiler so it knows it has now been included

// ==============================
// Forward declared dependencies

// ==============================
// Included dependencies
#include <iostream>
#include <fstream>

// ==============================
// Actual class
class Logfile { // Logfile
public:
    // Globally accessible variables
    bool LACT; // Is log file active?
    std::ofstream LOG; // Actual log file
    std::string entry; // Data to be entered in log file

    // ==============================
    // Core methods
    Logfile(); // Constructor
    ~Logfile(); // Destructor

    // Additional methods
    bool isActive(); // Is log file active?
    void logEntry(std::string entry); // Make an entry in the log if 'LACT' is set to true
    void toggleLog(); // Toggle log file open or closed
};

extern Logfile *log; // This variable is declared in main

#endif // __LOGFILE_INCLUDED__

日志文件.cpp

// ==============================
// Included dependencies
#include "Logfile.hpp"

// ==============================
// Core methods
Logfile::Logfile() { // Constructor
    LACT = true; // Toggle logfile to active
    LOG.open ("LOG.txt"); // Open 'log.txt' and prepare it to receive 'LOG' entries
    LOG << "LOG FILE CLASS CONSTRUCTED\n";
    LOG << "LOG FILE OPENED\n";
}

Logfile::~Logfile() { // Deconstructor
    LOG << "LOG FILE CLOSED\n";
    LOG << "LOG FILE CLASS DECONSTRUCTED";
    LOG.close(); // Close log file
}

// Additional methods
bool Logfile::isActive() { // Is log file active?
    if ( LACT ) return true;
    else return false;
}

void Logfile::logEntry(std::string entry) { // Make an entry in the log if 'LACT' is set to true
    if ( LACT ) LOG << entry << std::endl;
}

void Logfile::toggleLog() { // Toggle log file open or closed
    if ( LACT ) { // Log file is active
        LOG << "LOG FILE CLOSED\n";
        LOG.close(); // Close log file
    } else { // Log file is inactive
        LOG.open ("LOG.txt"); // Open 'log.txt' and prepare it to receive 'LOG' entries
        LOG << "LOG FILE OPENED\n";
    }
}

引擎.hpp

// ==============================
// Forward declared dependencies
class Logfile;

class Engine { // Core system, main loop
public :
    // Globally accessible variables 
    Logfile *log; // Declare 'log' as an empty pointer (*)

引擎.cpp

// ==============================
// Included dependencies
#include "Logfile.hpp"

// ==============================
// Core methods
Engine::Engine() { // Constructor method
    // Initialization
    log = new Logfile(); // Declare 'log' as pointer to access log file
    TCODConsole::initRoot(80,50,"Testbed",false); // Create 'root' console (not fullscreen)
    if ( log->isActive() ) log->logEntry("(TCODConsole) Root console initialized"); // WORKS

地图.hpp

// ==============================
// Forward declared dependencies
class Logfile;

extern Logfile *log; // Pointer exists in Engine.hpp

地图.cpp

// ==============================
// Included dependencies
#include "Logfile.hpp"

if ( log->isActive() ) log->logEntry("(TCODConsole) Root console initialized"); TERMINATION STATUS 3
if ( tiles[(x-1)+y*width].tType =="floor" ) tally++; // Left tile status, add 1 to tally if floor  :  TERMINATION STATUS 3
if ( tiles[(x-1)+(y-1)*width].canWalk ) tally++; // Left-top tile status, add 1 to tally if floor  :  WORKS

如果我理解正确,终止状态 3 表示我错误地引用了一个关于它是否是指针的变量......?当我想从 Map.cpp 中的 2Darray 中的单个图块访问 tType 字符串时,我最初遇到了这个问题(尽管我可以访问布尔变量 canWalk 就好了......),但不知道出了什么问题,所以我决定学习实现一个外部日志来查找问题......但我想我在这样做时找到了回到同一个问题的方法......

非常感谢任何帮助,就像批评一样,我还有很多东西要学。

--

我问这个问题的最初目的是(现在我意识到)从多文件程序中的任何 *.cpp 文件中获取一个全局声明的对象。我刚刚找到了这个答案:http ://www.cplusplus.com/forum/beginner/3848/ ,以防这可能对其他有类似问题的人有所帮助。

4

1 回答 1

1

在您的 Logfile.hpp 中,您缺少#include <string>:您的LogFile类变量之一已声明std::string,因此您需要包含它。

在您的 Engine.cpp 中,您忘记将您的 Engine.hpp 包含在您的 LogFile *log 中;变量被声明。这会导致您的 Engine.cpp 文件出现错误,您在该文件中尝试为其分配一个新LogFile对象。

所以添加#include <string>到 Logfile.hpp 的顶部并添加#include "Engine.hpp"到 Engine.cpp 的顶部

于 2014-02-15T03:15:16.163 回答