Jump to content
  • Advertisement
Sign in to follow this  
JasonL220

fstream error

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Make sure you #include <fstream>.

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.

Share this post


Link to post
Share on other sites
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:

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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
i changed the line to:

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.cpp
c:\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 operator
Build Time 0:02

Share this post


Link to post
Share on other sites
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 number
private:
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
};

Share this post


Link to post
Share on other sites
Is there a std::list of mem_info objects someplace? Perhaps you have defined a std::list<mem_info> when you're really wanting a std::list<mem_info*> or vice versa?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!