c++ class question

Started by
8 comments, last by alvaro 13 years, 7 months ago
i have

class ctest{public:	CString(const char *str)	{		*this+=str; // crash :S	}	~CString()	{	}	operator+=(const char*str)	{		for(size_t i=0; i<strlen(str); i++)		{			vec.push_back(str);		}	}private:	vector<char>vec;};void print(ctest str){	//...}

When i close the application it crash :S maybe i need to release something?

Advertisement
Quote:Original post by codder88
i have

*** Source Snippet Removed ***
When i close the application it crash :S maybe i need to release something?


hmm, i'm not sure about this but, what are the operator precedance for += and dereferencing ? (you might be adding the char pointer to this before you dereference it which would be a likely cause for a crash)

try:
(*this)+=str;

and see if that helps.

Edit: nvm , * should have higher priority than +=, it could be that your constructor and classnames doesn't match.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Quote:Original post by SimonForsman
Quote:Original post by codder88
i have

*** Source Snippet Removed ***
When i close the application it crash :S maybe i need to release something?


hmm, i'm not sure about this but, what are the operator precedance for += and dereferencing ? (you might be adding the char pointer to this before you dereference it which would be a likely cause for a crash)

try:
(*this)+=str;

and see if that helps.


it crash when i close the application :(
Does it even compile? You call your class ctest but your constructor and destructor is called CString. operator+= needs a return type. After fixing these errors it compiles and works fine.
class ctest{public:    ctest(const char *str)    {        *this+=str;    }    ~ctest()    {    }    ctest& operator+=(const char*str)    {        for(size_t i=0; i<strlen(str); i++)        {            vec.push_back(str);        }        return *this;    }private:    vector<char>vec;};
Quote:Original post by Wooh
Does it even compile? You call your class ctest but your constructor and destructor is called CString. operator+= needs a return type. After fixing these errors it compiles and works fine.
*** Source Snippet Removed ***


It's still crashing...
Quote:Original post by codder88
Quote:Original post by Wooh
Does it even compile? You call your class ctest but your constructor and destructor is called CString. operator+= needs a return type. After fixing these errors it compiles and works fine.
*** Source Snippet Removed ***


It's still crashing...


how do you create the class instance ?
is the char pointer you pass in valid ?
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Quote:Original post by codder88
It's still crashing...
And it's still not caused by the code you're posting.
There are all sorts of way the rest of your code can cause an error to show up here.

Your best options are to make a cut down minimal compile example program that produces the problem. Remove everything that isn't necessary to produce the problem.
Or you could learn to use your debugger and examine the call stack. What compiler/IDE?

One way or another, we need more info!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by iMalc
Quote:Original post by codder88
It's still crashing...
And it's still not caused by the code you're posting.
There are all sorts of way the rest of your code can cause an error to show up here.

Your best options are to make a cut down minimal compile example program that produces the problem. Remove everything that isn't necessary to produce the problem.
Or you could learn to use your debugger and examine the call stack. What compiler/IDE?

One way or another, we need more info!


I'm using VC++... I compiled in debug mode and there is an assertion failed
Expression: _CrtIsValidHeapPointer(pUserData)


The char* what i pass to ctest is valid...
How to fix it?
Quote:Original post by codder88
Quote:Original post by iMalc
Quote:Original post by codder88
It's still crashing...
And it's still not caused by the code you're posting.
There are all sorts of way the rest of your code can cause an error to show up here.

Your best options are to make a cut down minimal compile example program that produces the problem. Remove everything that isn't necessary to produce the problem.
Or you could learn to use your debugger and examine the call stack. What compiler/IDE?

One way or another, we need more info!


I'm using VC++... I compiled in debug mode and there is an assertion failed
Expression: _CrtIsValidHeapPointer(pUserData)


The char* what i pass to ctest is valid...
How to fix it?


Quote:Original post by iMalc
still not caused by the code you're posting.


How could we know?
Quote:Original post by phresnel
Quote:Original post by iMalc
still not caused by the code you're posting.


How could we know?


Well, the code he posted doesn't compile, so there's no way it causes a crash.

This topic is closed to new replies.

Advertisement