C++进程已终止,状态3混乱

C++ Process terminated with status 3 confusion

本文关键字:状态 混乱 终止 进程 C++      更新时间:2023-10-16

我对编程很陌生,但在过去一周左右的时间里,我一直在学习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__

Logfile.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 CONSTRUCTEDn";
LOG << "LOG FILE OPENEDn";
}
Logfile::~Logfile() { // Deconstructor
LOG << "LOG FILE CLOSEDn";
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 CLOSEDn";
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 OPENEDn";
}
}

发动机.hpp

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

Engine.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

Map.hpp

// ==============================
// Forward declared dependencies
class Logfile;
extern Logfile *log; // Pointer exists in Engine.hpp

Map.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中的单个tile访问tType字符串时,我最初遇到了这个问题(尽管我可以访问布尔变量canWalk…),但我不知道出了什么问题,所以我决定学习实现外部日志来查找问题。。。但我想我在做这件事的时候找到了回到同一个问题的方法。。。

任何帮助都会受到极大的赞赏,批评也是如此,我还有很多东西要学。

--

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

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

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

因此,将#include <string>添加到Logfile.hpp的顶部,并将#include "Engine.hpp"添加到Engine.cpp 的顶部