#1 Members - Reputation: 343
Posted 21 October 2012 - 02:08 PM
I am learning SDL and i thought it would be helpful to make an Debug log to tell me if something is working or not. I am doing the project as a stand alone just to see if it works and then implement it in my SDL projects. So i have played a round with it a little but i cannot get the program to work. I am trying to make a class that handles all the debug stuff for me using the fstream to make a file telling me what happened. I cannot seem to get this to work, can anyone give me some hints? anything would be much appreciated.
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#3 Members - Reputation: 343
Posted 21 October 2012 - 02:58 PM
Main.cpp
[source lang="cpp"]#include "stdafx.h"#include "DebugLog.h"int main (){DebugLog debug("DebugLog.txt");debug.~debug();return 0;}[/source]
DebugLog.h
[source lang="cpp"]#include "stdafx.h"#include <fstream>#include <iostream>#include <string>using namespace std;class DebugLog {public:DebugLog();~DebugLog();string writeTo(string text);private:string m_Text;string m_Filename;};[/source]
DebugLog.cpp
[source lang="cpp"]#include "stdafx.h"#include "DebugLog.h"DebugLog::DebugLog(){ofstream debugLog("DebugLog.txt");m_Text = "-----Debug Log Starts Now-----\n\n";debugLog << m_Text;}DebugLog::~DebugLog(){m_Text = "-----End of DebugLog-----";debugLog << m_Text;debugLog.close();}string DebugLog::writeTo(string text){m_Text = text;debugLog << text;return;}[/source]
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#4 Members - Reputation: 343
Posted 21 October 2012 - 03:00 PM
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#6 Members - Reputation: 343
Posted 21 October 2012 - 03:10 PM
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class DebugLog {
public:
DebugLog();
~DebugLog();
string writeTo(string text);
private:
string m_Text;
string m_Filename;
};
Debug.cpp:
#include "stdafx.h"
#include "DebugLog.h"
DebugLog::DebugLog(){
ofstream debugLog("DebugLog.txt");
m_Text = "-----Debug Log Starts Now-----\n\n";
debugLog << m_Text;
}
DebugLog::~DebugLog(){
m_Text = "-----End of DebugLog-----";
debugLog << m_Text;
debugLog.close();
}
string DebugLog::writeTo(string text){
m_Text = text;
debugLog << text;
return;
}
and Main.cpp
{
DebugLog debug("DebugLog.txt");
debug.writeTo("Line 1\n");
debug.~debug();
return 0;
I have an idea about whats going on but not a way to fix it.
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#7 Crossbones+ - Reputation: 3517
Posted 21 October 2012 - 03:17 PM
Secondly, does this even compile? debugLog is not defined in the destructor or in writeTo(). Basically, you allocate your iostream in the constructor, which is on the stack - as soon as it goes out of scope (i.e. when the constructor returns) the iostream is closed and lost. This iostream exists only in the constructor. If you want to keep a persistent iostream throughout your debug log's lifetime, you want it to exist as a private field in your class. Then you can use it anywhere, and your writeTo() will actually work. Does it make sense?
#8 Members - Reputation: 343
Posted 21 October 2012 - 03:30 PM
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#9 Members - Reputation: 343
Posted 21 October 2012 - 03:32 PM
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#10 Crossbones+ - Reputation: 3517
Posted 21 October 2012 - 03:52 PM
This means the debugLog object is now attached to your class instead of a specific method, so every method in the class has now access to it. The constructor takes care of creating the stream. Does it make more sense now?
Never mind the destructor stuff for now - you need to understand the concept of class fields before.
Edited by Bacterius, 21 October 2012 - 03:55 PM.
#11 Members - Reputation: 343
Posted 21 October 2012 - 04:05 PM
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#12 Members - Reputation: 343
Posted 21 October 2012 - 04:11 PM
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#13 Members - Reputation: 343
Posted 21 October 2012 - 04:15 PM
:Unhandled exception at 0x7784ef10 in DebugLog.exe: 0xC0000005: Access violation reading location 0xfeeefef6.
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#14 Crossbones+ - Reputation: 3517
Posted 21 October 2012 - 04:19 PM
What did you mean by that?And int the main function it says DebugLog debug("DebugLog.txt"), that isnt supposed to be there sorry i forgot to delete it.
#15 Members - Reputation: 343
Posted 21 October 2012 - 04:25 PM
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#16 Members - Reputation: 343
Posted 21 October 2012 - 04:28 PM
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#17 Crossbones+ - Reputation: 3517
Posted 21 October 2012 - 04:38 PM
[source lang="cpp"]#include < iostream >#include < fstream >using namespace std;class DebugLog {public:DebugLog();void writeTo(string text);~DebugLog();private:ofstream m_debugLog;};DebugLog::DebugLog(){ m_debugLog.open("DebugLog.txt");}DebugLog::~DebugLog(){ cout < < "Closing debug log." < < endl; m_debugLog.close();}void DebugLog::writeTo(string text){ m_debugLog < < text;}int main(){ DebugLog log; log.writeTo("hello world"); // log is about to fall out of scope - destructor should be called return 0;}[/source]
Remove all the extra spaces I had to insert since the source tags are broken. I think we're just having a communication breakdown here.
Edited by Bacterius, 21 October 2012 - 04:39 PM.
#18 Members - Reputation: 343
Posted 21 October 2012 - 05:14 PM
"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"
"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"
#19 Crossbones+ - Reputation: 3517
Posted 21 October 2012 - 05:19 PM






