🔒 Locked
fstream error
Advertisement
Original Post
the line that is causing the error is: fstream mem("leaks.txt"); and the error is: c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(57) : error C2440: 'initializing' : cannot convert from 'const char [10]' to 'int' There is no context in which this conversion is possible what the hell is that about why do i need to pass an int? could someone please explain? btw i'm using std::fstream
Make sure you #include .
For future reference, please post all of the errors your compiler spews out, as well as more of your code.
[edit] I say this because the previous error was probably c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(57) : error C2079: 'mem' uses undefined class 'std::basic_fstream<_Elem,_Traits>', which would have given you the answer.
For future reference, please post all of the errors your compiler spews out, as well as more of your code.
[edit] I say this because the previous error was probably c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(57) : error C2079: 'mem' uses undefined class 'std::basic_fstream<_Elem,_Traits>', which would have given you the answer.
No need to apologize. We've all been there.
this not related to the previous error but its in the same project so i didn't want to start a new tread.
this is my time using exceptions, so maybe i'm missing something stupid.
anyway here is the whole func that is generating the error:
i only have 1 error:
leaky.cpp
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(17) : error C2275: 'std::bad_alloc' : illegal use of this type as an expression
c:\program files\microsoft visual studio 8\vc\include\exception(334) : see declaration of 'std::bad_alloc'
Build Time 0:00
Build log was saved at "file://c:\Documents and Settings\Jason\My Documents\Visual Studio 2005\Projects\leaky\leaky\Debug\BuildLog.htm"
leaky - 1 error(s), 0 warning(s)
this is my time using exceptions, so maybe i'm missing something stupid.
anyway here is the whole func that is generating the error:
inline void * __cdecl operator new(size_t size, const char *file, int line){ try { // Allocate the memory check that the allocation was a success and add to the tracker if(size == 0) { size = 1; // Make sure a size of 0 is not sent to malloc } void *ptr = (void *)malloc(size); if(ptr == 0) { throw bad_alloc; } mem_info *m_info = new mem_info(ptr, size, file, line); AddTracker(m_info); return(ptr); } catch(bad_alloc) { // Handle an error fstream mem("leaks.txt"); mem << "MEMORY ALLOCATION ERROR" << "\n"; }};i only have 1 error:
leaky.cpp
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(17) : error C2275: 'std::bad_alloc' : illegal use of this type as an expression
c:\program files\microsoft visual studio 8\vc\include\exception(334) : see declaration of 'std::bad_alloc'
Build Time 0:00
Build log was saved at "file://c:\Documents and Settings\Jason\My Documents\Visual Studio 2005\Projects\leaky\leaky\Debug\BuildLog.htm"
leaky - 1 error(s), 0 warning(s)
The line throw badalloc; is not correct; strictly, by the syntax, this would throw the entire class std::badalloc which doesn't make sense. Instead, you want to throw a temporary instance of std::badalloc, which means you need to invoke a constructor. You can create an anonymous instance easily by tweaking the line to throw badalloc(); which has the effect of calling the default constructor for std::badalloc, generating a temporary unnamed instance, and throwing that as the exception.
i changed the line to:
but i get these error now:
inline void * __cdecl operator new(size_t size, const char *file, int line){ try { // Allocate the memory check that the allocation was a success and add to the tracker if(size == 0) { size = 1; // Make sure a size of 0 is not sent to malloc } void *ptr = (void *)malloc(size); if(ptr == 0) { throw bad_alloc(); } mem_info *m_info = new mem_info(ptr, size, file, line); AddTracker(m_info); return(ptr); } catch(bad_alloc) { // Handle an error fstream mem("leaks.txt"); mem << "MEMORY ALLOCATION ERROR" << "\n"; }};but i get these error now:
leaky.cppc:\program files\microsoft visual studio 8\vc\include\list(901) : error C2784: 'bool std::operator ==(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'mem_info' c:\program files\microsoft visual studio 8\vc\include\list(1257) : see declaration of 'std::operator ==' c:\program files\microsoft visual studio 8\vc\include\list(898) : while compiling class template member function 'void std::list<_Ty>::remove(const _Ty &)' with [ _Ty=mem_info ] c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(3) : see reference to class template instantiation 'std::list<_Ty>' being compiled with [ _Ty=mem_info ]c:\program files\microsoft visual studio 8\vc\include\list(901) : error C2784: 'bool std::operator ==(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'mem_info' c:\program files\microsoft visual studio 8\vc\include\xmemory(174) : see declaration of 'std::operator =='c:\program files\microsoft visual studio 8\vc\include\list(901) : error C2784: 'bool std::operator ==(const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &,const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &)' : could not deduce template argument for 'const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &' from 'mem_info' c:\program files\microsoft visual studio 8\vc\include\iterator(266) : see declaration of 'std::operator =='c:\program files\microsoft visual studio 8\vc\include\list(901) : error C2784: 'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'mem_info' c:\program files\microsoft visual studio 8\vc\include\xutility(2143) : see declaration of 'std::operator =='c:\program files\microsoft visual studio 8\vc\include\list(901) : error C2784: 'bool std::operator ==(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'mem_info' c:\program files\microsoft visual studio 8\vc\include\xutility(1826) : see declaration of 'std::operator =='c:\program files\microsoft visual studio 8\vc\include\list(901) : error C2784: 'bool std::operator ==(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'mem_info' c:\program files\microsoft visual studio 8\vc\include\utility(60) : see declaration of 'std::operator =='c:\program files\microsoft visual studio 8\vc\include\list(901) : error C2676: binary '==' : 'mem_info' does not define this operator or a conversion to a type acceptable to the predefined operatorBuild Time 0:02
What is mem_info?
sorry mem_info is defined as the following:
class mem_info{public: mem_info(void *mptr, size_t msize, const char* mfile, int mline) : ptr(mptr), size(msize), file(mfile), line(mline) {}; // Set up the class ~mem_info(); // Cleanup the class void * GetPointer(); // Get the address of the object size_t GetSize(); // Get the size of the object const char* GetFile(); // Get the file name int GetLine(); // Get the line numberprivate: void *ptr; // The address of the allocated memory size_t size; // The size on the memory allocated const char* file; // The file in which the allocation accured int line; // The line in which the allocation accured};
i'll post all the code:
leaky.cpp
leaky.h
i have a list tracker;
leaky.cpp
[source land="cpp"]#include "leaky.h"list<mem_info> tracker;inline void * __cdecl operator new(size_t size, const char *file, int line){ try { // Allocate the memory check that the allocation was a success and add to the tracker if(size == 0) { size = 1; // Make sure a size of 0 is not sent to malloc } void *ptr = (void *)malloc(size); if(ptr == 0) { throw bad_alloc(); } mem_info *m_info = new mem_info(ptr, size, file, line); AddTracker(m_info); return(ptr); } catch(bad_alloc) { // Handle an error fstream mem("leaks.txt"); mem << "MEMORY ALLOCATION ERROR" << "\n"; }};inline void __cdecl operator delete(void *p){ RemoveTracker((void *)p); free(p);};void AddTracker(mem_info minfo){ tracker.push_back(minfo);};void RemoveTracker(void *ptr){ list<mem_info>::iterator iter; for(iter = tracker.begin(); iter != tracker.end(); iter++) { if((*iter).GetPointer() == ptr) { tracker.remove((*iter)); break; } }};void ReportLeaks(){ list<mem_info>::iterator iter; fstream mem("leaks.txt"); // Report the memory leaks mem << "\tAddress\t" << "\tSize\t" << "\tFile\t" << "\tLine\t" << "\n\n"; mem << "\t----------" << "----------" << "----------" << "----------" << "\n\n"; for(iter = tracker.begin(); iter != tracker.end(); iter++) { mem << "\t" << (*iter).GetPointer() << "\t\t" << (*iter).GetSize() << "\t\t" << (*iter).GetFile() << "\t\t" << (*iter).GetLine() << "\t\n\n"; } for(iter = tracker.begin(); iter != tracker.end(); iter++) { free((*iter).GetPointer()); // Make sure that no memory is left allocated } mem.close();};// return file nameconst char* mem_info::GetFile(){ return file;};// return line numberint mem_info::GetLine(){ return line;};// return address of objectvoid * mem_info::GetPointer(){ return ptr;};// return size of objectsize_t mem_info::GetSize(){ return size;};leaky.h
/////////////////////////////////////////////////////////////////////// Detect memory leaks ///////////////////////////////////////////////////////////////////////#include <cstddef> // For size_t#include <list> // For list#include <stdlib.h> // For malloc and free#include <stdexcept> // For bad_alloc#include <fstream> // For file accessusing std::size_t;using std::bad_alloc;using std::list;using std::fstream;// replace the default new with new modified version#ifdef MEM_DEBUG#define new new(__FILE__, __LINE__)#endifinline void * __cdecl operator new(size_t size, const char *file, int line);inline void __cdecl operator delete(void *p);class mem_info{public: mem_info(void *mptr, size_t msize, const char* mfile, int mline) : ptr(mptr), size(msize), file(mfile), line(mline) {}; // Set up the class ~mem_info(); // Cleanup the class void * GetPointer(); // Get the address of the object size_t GetSize(); // Get the size of the object const char* GetFile(); // Get the file name int GetLine(); // Get the line numberprivate: void *ptr; // The address of the allocated memory size_t size; // The size on the memory allocated const char* file; // The file in which the allocation accured int line; // The line in which the allocation accured};void AddTracker(mem_info *minfo); // Add the allocated memory info to the listvoid RemoveTracker(void *ptr); // Remove the allocated memory info from the listvoid ReportLeaks(); // Report the leaksi have a list
You need to provide an equality operator. I guess the easiest way is like so:
class mem_info {
public:
// ...
bool operator==( const mem_info &other ) const {
return this->ptr == other.ptr;
}
// ...
};
I don't know if this is the right way to go about it in the grand scheme of things, though...
jfl.
class mem_info {
public:
// ...
bool operator==( const mem_info &other ) const {
return this->ptr == other.ptr;
}
// ...
};
I don't know if this is the right way to go about it in the grand scheme of things, though...
jfl.
sorry to keep on but i have a linking error, i've read that it is to do with not referencing a function but have no idea what to do to fix it. the error is:
------ Build started: Project: leaky, Configuration: Debug Win32 ------
Compiling...
leaky.cpp
Linking...
leaky.obj : error LNK2019: unresolved external symbol "public: __thiscall mem_info::~mem_info(void)" (??1mem_info@@QAE@XZ) referenced in function "void __cdecl AddTracker(class mem_info)" (?AddTracker@@YAXVmem_info@@@Z)
C:\Documents and Settings\Jason\My Documents\Visual Studio 2005\Projects\leaky\Debug\leaky.exe : fatal error LNK1120: 1 unresolved externals
Build Time 0:00
Build log was saved at "file://c:\Documents and Settings\Jason\My Documents\Visual Studio 2005\Projects\leaky\leaky\Debug\BuildLog.htm"
leaky - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
------ Build started: Project: leaky, Configuration: Debug Win32 ------
Compiling...
leaky.cpp
Linking...
leaky.obj : error LNK2019: unresolved external symbol "public: __thiscall mem_info::~mem_info(void)" (??1mem_info@@QAE@XZ) referenced in function "void __cdecl AddTracker(class mem_info)" (?AddTracker@@YAXVmem_info@@@Z)
C:\Documents and Settings\Jason\My Documents\Visual Studio 2005\Projects\leaky\Debug\leaky.exe : fatal error LNK1120: 1 unresolved externals
Build Time 0:00
Build log was saved at "file://c:\Documents and Settings\Jason\My Documents\Visual Studio 2005\Projects\leaky\leaky\Debug\BuildLog.htm"
leaky - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement