C++进程已终止,状态3混乱
C++ Process terminated with status 3 confusion
我对编程很陌生,但在过去一周左右的时间里,我一直在学习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 的顶部
- 如何克服提升精神AST混乱
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- OSX MetalKit CVMetalTextureCacheCreateTextureFromImage失败,状态:
- std::future_error:无关联状态
- 如何避免LED在循环状态变化中闪烁?
- boost 是否有按特殊类型值编码状态"compact optional"?
- 为什么系统函数总是在C++中返回已转移的退出状态?
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 当可输入框在窗口中处于活动状态时获得通知的任何方法
- 检查两个节点在子节点上是否具有相同状态的更优雅的方法
- 将有状态的 lambda 传递到 C 样式函数中,而无需上下文参数
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 试图在崇高中奔跑. 错误 已发生: 收集2.exe: 错误: ld 返回 1 退出状态
- 在容器上移动分配:以前包含的对象的状态
- collect2:错误:ld 返回 1 个退出状态未定义的引用
- C++进程已终止,状态3混乱
- wchar_t数据从非托管状态混乱到托管状态