I have declared a class and instantiated a class in one and expected it to fire
~CLog();
But for some reason, it does not. Does anybody see any obvious errors why this could happen?
I declared the class within a void that ends, so it SHOULD fire, I think.
I do not destroy the class explicitely, but I simply expected it to go out of out scope automatically and terminate.
#pragma once
#include <fstream>
using namespace std;
class CLog
{
public:
CLog(wstring filename);
~CLog();
void WriteString(wstring uString);
private:
wofstream m_stream;
wstring m_sPath;
};
#include "log.h";
#include "strhelper.h"
#include <fstream>
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
wstring m_sText=L"";
wstring m_sPath=L"";
CLog::CLog(wstring uPath)
{
m_sPath=uPath;
}
void CLog::WriteString(wstring uString)
{
m_sText+=uString;
m_sText+=L"\n";
}
CLog::~CLog()
{
if (FileExists(m_sPath))
{
DeleteFile(m_sPath);
}
//open for appending
m_stream.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t,0x10ffff,std::generate_header>));
m_stream.open(m_sPath,fstream::in | fstream::out | fstream::app);
m_stream << m_sText.c_str();
m_stream.close();
}
I am using Clog like this
void foo() {
wstring sLogPath;
sLogPath=GetSpecialFolderDesktop() + L"\\load.log";
CLog *pLog = new CLog(sLogPath);
pLog->WriteString(L"Something);
}
I am using VC2010.
You are instantiating dynamically the CLog. In that case, you need to delete it explicitly.
If you create it on the stack Clog log(sLogPath), the destructor will be called when the object goes out of scope.
Related
main.cpp
#include <iostream>
#include "shreeman.h"
using namespace std;
int main(){
Shreeman object;
}
shreeman.h
#ifndef SHREEMAN_H
#define SHREEMAN_H
class Shreeman
{
public:
shreeman();
};
#endif // SHREEMAN_H
shreeman.cpp
#include <iostream>
#include "shreeman.h"
using namespace std;
Shreeman::shreeman()
{
cout<<"Hello Hello"<<endl;
}
Why does this code not output "Hello Hello"? I have created an object in the main.cpp file, yet nothing is printed in the console.
In the Shreeman class, shreeman() (lowercase s) is not a valid constructor, it is just a member method. It needs to be renamed to Shreeman() (uppercase S) to be a construcor (the compiler should have warned you about that). C++ is case-sensitive, and a constructor name needs to match the class name exactly, case and all.
main.cpp
#include "shreeman.h"
int main()
{
Shreeman object;
}
shreeman.h
#ifndef SHREEMAN_H
#define SHREEMAN_H
class Shreeman
{
public:
Shreeman();
};
#endif // SHREEMAN_H
shreeman.cpp
#include <iostream>
#include "shreeman.h"
using namespace std;
Shreeman::Shreeman()
{
cout << "Hello Hello" << endl;
}
Error: multiple definition of `GameKey::getGameKeywords()'
GameKey.cpp and .h cause error, while ExitKey.cpp and .h are essentially the exact same class and header but do not produce an error.
(I know the whole thing about using namespace std)
//Function Declarations
#ifndef GAMEKEY_H
#define GAMEKEY_H
// C++ libraries
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
class GameKey
{
private:
string keyString;
string lineData;
public:
// Default constructor
GameKey();
// Deconstructor
~GameKey();
// Get keywords
string getGameKeywords();
};
#endif
GameKey.cpp
//Function Definitions
#include "GameKey.h"
// Constructor
GameKey::GameKey()
{
}
// Deconstructor
GameKey::~GameKey()
{
}
// Get keywords
string GameKey::getGameKeywords()
{
ifstream infile;
infile.open("GameKey.txt");
while (getline(infile, lineData))
{
keyString.append(lineData);
keyString.append("\n");
}
infile.close();
return keyString;
}
ExitKey.h
//Function Declarations
#ifndef EXITKEY_H
#define EXITKEY_H
// C++ libraries
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
class ExitKey
{
private:
string keyString;
string lineData;
public:
// Default constructor
ExitKey();
// Deconstructor
~ExitKey();
// Get keywords
string getExitKeywords();
};
#endif
ExitKey.cpp
//Function Definitions
#include "ExitKey.h"
// Constructor
ExitKey::ExitKey()
{
}
// Deconstructor
ExitKey::~ExitKey()
{
}
// Get keywords
string ExitKey::getExitKeywords()
{
ifstream infile;
infile.open("ExitKey.txt");
while (getline(infile, lineData))
{
keyString.append(lineData);
keyString.append("\n");
}
infile.close();
return keyString;
}
Thanks for any help!
I think you probably include GameKey.cpp instead of GameKey.h elsewhere
I am not certain as the command used for compilation is not posted.
One possibility is repeating the file names in your compilation command could also lead to this error.
for example :-
g++ ExitKey.cpp GameKey.cpp GameKey.cpp main.cpp -o main
I need ostream pointer in class which will be created at the time of construction of class.
My code is :
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cstring>
#include <climits>
#include <cstdio>
#include <fstream>
using namespace std;
class test2_t {
public:
test2_t ()
{
std::filebuf fb;
fb.open ("dump.txt",std::ios::out);
ostream *output_ = new std::ostream(&fb);
}
virtual ~test2_t ()
{}
ostream *output_;
void printing()
{
print(output_);
}
void print(ostream *out)
{
*out<<"dump data"<<"\n";
}
private:
/* data */
};
int main( )
{
test2_t obj;
obj.printing();
}
But is getting Segmentation fault I don't know why. Please help me out.
You made the following mistake in your code:
You "redeclared" your "Output"-variable in the constructor - so the pointer ios only stored in a local variable within the constructor-scope.
change this line: ostream *output_ = new std::ostream(&fb);
into: *output_ = new std::ostream(&fb);
In this way, the member-variable of your class is filled with the correkt pointer.
You can change your construtor function like follows to get it working:
test2_t () : output_(new std::ofstream("dump.txt")) {
}
Don't forget to release the resource in the destructor:
virtual ~test2_t () {
delete output_;
}
I'm trying to use arrays with unique_ptr with no success.
What is the correct way to declare a unique_ptr of some size?
(size is some paramter).
unique_ptr<A[]> ptr = make_unique<A[]>(size);
Here's an example:
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <memory>
using namespace std;
class A {
string str;
public:
A(string _str): str(_str) {}
string getStr() {
return str;
}
};
int main()
{
unique_ptr<A[]> ptr = make_unique<A[]>(3);
}
This is not working, however, if I delete the constructor of A, it works.
I want the 3 to represent the size of the array, and not an argument to A's constructor, how do I make that happen?
This is not working, however, if I delete the constructor of A, it
works.
When you removed the user defined constructor, the compiler implicitly generates a default one. When you provide a user defined constructor, the compiler doesn't implicitly generate a default constructor.
std::make_unique<T[]> requires the use of default constructors...
So, provide one, and all should work well
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <memory>
using namespace std;
class A {
string str;
public:
A() = default;
A(string _str): str(_str) {}
string getStr() {
return str;
}
};
int main()
{
unique_ptr<A[]> ptr = make_unique<A[]>(3);
}
I have a simple logger class which I tried to turn into accepting and outputting wstrings instead strings.
header:
#include <fstream>
using namespace std;
class CLog
{
public:
CLog(wstring filename);
~CLog();
void WriteString(string uString);
private:
fstream m_stream;
};
cpp:
#include "stdafx.h";
#include "log.h";
CLog::CLog(wstring uPath)
{
m_stream.open(uPath);
}
void CLog::WriteString(string uString)
{
m_stream << uString.c_str() << endl;
}
CLog::~CLog()
{
m_stream.close();
}
Can anybody suggest what I should use instead of fstream?
I tried using wstringstream, but it did not even have .open to output it to file, so I thought that is the wrong approach.
I would like to keep the behaviour that it immediately writes to a file.
I use "wofstream" instead of "fstream" now, and it works perfectly.