C ++进程以状态3混乱终止 [英] C++ Process terminated with status 3 confusion
问题描述
我是编程的新手,但一直在下面的c ++教程,积累了一些PDF的最后一周左右帮助我。我没有找到任何在他们或在线,回答我的问题清楚够了。请原谅我的新手。
相关代码:
Logfile.hpp
// = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =
// HEADER :: CLASS INTERFACE FILE
// ============================
//包括警卫
#ifndef __LOGFILE_INCLUDED__ / /如果Actor.hpp尚未包含
#define __LOGFILE_INCLUDED__ //为编译器定义此编译器,因此它知道它现在已被包括
// ======= =====================
//转发声明的依赖项
// ======== ====================
//包含的依赖项
#include< iostream>
#include< fstream>
// ============================
//实际等级
class Logfile {// Logfile
public:
//全局可访问变量
bool LACT; //日志文件是否激活?
std :: ofstream LOG; //实际日志文件
std :: string entry; //在日志文件中输入的数据
// ============================ b $ b //核心方法
Logfile(); // Constructor
〜Logfile(); // Destructor
//其他方法
bool isActive(); //日志文件是否活动?
void logEntry(std :: string entry); //如果'LACT'设置为true,在日志中创建一个条目
void toggleLog(); //切换日志文件打开或关闭
};
extern Logfile * log; //此变量在main中声明
#endif // __LOGFILE_INCLUDED__
Logfile.cpp
// ======================= =========
//包含的依赖项
#includeLogfile.hpp
// ============ ==================
//核心方法
Logfile :: Logfile(){//构造函数
LACT = true; //将日志文件切换为活动
LOG.open(LOG.txt); //打开log.txt并准备接收LOG条目
LOG<< LOG FILE CLASS CONSTRUCTED\ n;
LOG<< LOG FILE OPENED\ n;
}
Logfile ::〜Logfile(){//解构函数
LOG< LOG FILE CLOSED\\\
;
LOG<< LOG FILE CLASS DECONSTRUCTED;
LOG.close(); //关闭日志文件
}
//其他方法
bool Logfile :: isActive(){//日志文件是否活动?
if(LACT)return true;
else return false;
}
void Logfile :: logEntry(std :: string entry){//如果'LACT'设置为true,在日志中输入一个条目
if(LACT) LOG<条目<< std :: endl;
}
void Logfile :: toggleLog(){//切换日志文件打开或关闭
if(LACT){//日志文件是活动的
LOG< ;& LOG FILE CLOSED\\\
;
LOG.close(); //关闭日志文件
} else {//日志文件不活动
LOG.open(LOG.txt); //打开log.txt并准备接收LOG条目
LOG<< LOG FILE OPENED\ n;
}
}
Engine.hpp
// ==========================
//转发声明的依赖关系
class Logfile;
class Engine {//核心系统,主循环
public:
//全局可访问变量
Logfile * log; //将log声明为空指针(*)
Engine.cpp
// ============================= =
//包含的依赖项
#includeLogfile.hpp
// ==================== ==========
//核心方法
Engine :: Engine(){//构造方法
//初始化
log = new Logfile() ; //声明'log'作为访问日志文件的指针
TCODConsole :: initRoot(80,50,Testbed,false); //创建'root'控制台(不是全屏)
if(log-> isActive())log-> logEntry((TCODConsole) // WORKS
Map.hpp
// ============================
//转发声明的依赖
class Logfile;
extern Logfile * log; // Pointer exists in Engine.hpp
Map.cpp
// ============================
//包含的依赖项
#includeLogfile.hpp
if(log-> isActive())log-> logEntry((TCODConsole)终止状态3
if(tiles [(x-1)+ y * width] .tType ==floor)tally ++; //如果floor:TERMINATION STATUS 3
if(tiles [(x-1)+(y-1)* width]。 // Left-top瓦片状态,如果floor:WORKS
如果我理解正确,终止状态3指示我参考一个变量不正确地关于它是否是一个指针...?我最初遇到了这个问题,当我想访问从2Darray在Map.cpp中的一个单独的瓷砖tType字符串(虽然我可以访问布尔变量canWalk只是罚款...),并不能弄清楚什么是错的,所以我决定学习实现一个外部日志来找到问题...但我想我发现我的方式回到同样的问题,同时...
-
我提出这个问题的最初目的是(现在我意识到)从多文件程序中的任何* .cpp文件获取一个全局声明的对象。我刚找到这个答案: http://www.cplusplus.com/forum/beginner/3848/ 。
在您的Logfile.hpp中, 're missing #include< string>
:您的 LogFile
类变量之一声明为 std :: string
,所以你需要包括它。
在您的Engine.cpp中,您忘记在您的LogFile *日志中包括您的Engine.hpp;变量被声明。这将导致您的Engine.cpp文件中的错误,您尝试为其分配一个新的 LogFile
对象。
因此,将 #include< string>
添加到Logfile.hpp的顶部,并添加 #includeEngine.hpp
到您的Engine.cpp
的顶部
I am very new to programming, but have been following c++ tutorials and amassing a number of PDFs for the last week or so to help me out. I couldn't find anything in them or online that answered my question clearly enough. Please forgive me for my newbie-ness.
Pertinent code:
Logfile.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 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";
}
}
Engine.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
If I understand correctly, a termination status 3 indicates that I am referencing a variable incorrectly in regards to whether it's a pointer or not...? I initially ran into the problem when I wanted to access the tType string from an individual tile in the 2Darray in Map.cpp (though I can access the boolean variable canWalk just fine...), and couldn't figure out what was wrong, so I decided to learn to implement an external log to find the problem...but I guess I found my way back to the same issue while doing that...
Any help is greatly appreciated, as is criticism, I have a lot to learn.
--
My initial purpose for asking this question was (now I realize) to get a globally declared object accessible from any *.cpp file in a multi-file program. I just found this answer: http://www.cplusplus.com/forum/beginner/3848/, in case this might be helpful to anyone else with a similar problem.
In your Logfile.hpp you're missing #include <string>
: one of your LogFile
classes variables are declared std::string
so you need to include that.
In your Engine.cpp you forget to include your Engine.hpp where your LogFile *log; variable is declared. this results in an error in your Engine.cpp file where you try assigning a new LogFile
object to it.
So add #include <string>
to the top of your Logfile.hpp and add #include "Engine.hpp"
to the top of your Engine.cpp
这篇关于C ++进程以状态3混乱终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!