Jump to content

  • Log In with Google      Sign In   
  • Create Account


c++ class question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 codder88   Members   -  Reputation: 103

Like
0Likes
Like

Posted 06 September 2010 - 12:55 PM

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[i]);
}
}

private:
vector<char>vec;
};

void print(ctest str)
{
//...
}



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



Sponsor:

#2 SimonForsman   Crossbones+   -  Reputation: 5786

Like
0Likes
Like

Posted 06 September 2010 - 01:05 PM

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.

#3 codder88   Members   -  Reputation: 103

Like
0Likes
Like

Posted 06 September 2010 - 01:08 PM

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 :(

#4 Wooh   Members   -  Reputation: 567

Like
0Likes
Like

Posted 06 September 2010 - 01:09 PM

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[i]);
}
return *this;
}
private:
vector<char>vec;
};



#5 codder88   Members   -  Reputation: 103

Like
0Likes
Like

Posted 06 September 2010 - 01:16 PM

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...

#6 SimonForsman   Crossbones+   -  Reputation: 5786

Like
0Likes
Like

Posted 06 September 2010 - 01:20 PM

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 ?

#7 iMalc   Crossbones+   -  Reputation: 2260

Like
0Likes
Like

Posted 06 September 2010 - 08:56 PM

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!

#8 codder88   Members   -  Reputation: 103

Like
0Likes
Like

Posted 07 September 2010 - 01:13 AM

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?

#9 phresnel   Members   -  Reputation: 949

Like
0Likes
Like

Posted 07 September 2010 - 01:16 AM

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?

#10 Álvaro   Crossbones+   -  Reputation: 11905

Like
0Likes
Like

Posted 07 September 2010 - 01:45 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS