Question:
C++ Help please... don't know whats wrong with my code. Noob Question?
Juan
2012-04-14 12:53:10 UTC
Im having a lot of trouble figuring out what's wrong in my code...
it compiles and everything but every time I try to run the program it crashes before even starting and displays the following message:

Debug Assertion Failed!

Program: c:\CIS 278\MyString\Debug\MyString.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
Line: 52

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

For information..........

I'm trying to finish this school project before the end of today, so please any help will be greatly appreciated.

Here's a copy of my Code:

//Project 6 by Juan Hernandez
//CIS 278
// This is the MyString.cpp file
#include
#include "MyString.h"
using namespace std;

MyString::MyString(){
str = new char[9];
str ="Default";
length = strlen(str);
}

MyString::MyString(char newstr[]){
length = strlen(newstr);
delete [] str;
str = new char[length];
for (int i = 0; i < length ; i++)
str[i] = newstr[i];
}
MyString::MyString(const MyString& obj) : length(obj.getLen()){
delete [] str;
str = new char[length];
for(int i = 0; i < obj.getLen() ; i++)
str[i] = obj.getElement(i);
}
MyString::~MyString(){
delete [] str;
}
int MyString:: getLen() const{
return strlen(str);
}
void MyString::setElement(int index, char item){
str[index] = item;
}
char MyString::getElement(int index) const{
return str[index];
}
bool MyString::operator==(const MyString& obj){
bool answer = true;
if(length != obj.getLen())
answer = false;
do{
for(int i = 0; i < obj.getLen() ; i++)
if ( str[i] != obj.getElement(i))
answer = false;
}while(answer == true);
return answer;
}
bool MyString::operator !=(const MyString& obj){
bool answer = false;
if(length != obj.getLen())
answer = true;
do{
for(int i = 0; i < obj.getLen() ; i++)
if ( str[i] != obj.getElement(i))
answer = true;
}while(answer == false);
return answer;
}
char& MyString::operator [](int in){
if((in < 0) || (in > length)){
cout << "Error, contact software manufacturer for more info." << endl;
exit(1);}
else
return str[in];
}
MyString& MyString::operator=(const MyString& obj){
if(this == &obj){
return *this;
}
else{
length = obj.getLen();
delete [] str;
str = new char[length];
for(int i = 0 ; i < length ; i++)
str[i] = obj.getElement(i);

return *this;
}
}
ostream& operator<<(ostream& cout, const MyString& obj){
cout << obj.str;
return cout;
}
Three answers:
Unca Alby
2012-04-14 14:44:49 UTC
Why don't you put your code up on pastebin.com and display a link to it here.



Also include "MyString.h", which may possibly be the source of your problem.



Otherwise, there's not enough information for good debugging.



EDIT -- yah, that's a lot better -- what kind of compiler are you using? that should NOT compile!!



I tried gcc version 4.1.2 on a CentOS Linux, AND gcc version 4.6.1 on MinGW PC:



MyString.h:11: error: ‘CharPtr’ does not name a type



This is because you have:

char* CharPtr; -- you are declaring a variable "CharPtr" to be a pointer to a char

CharPtr str; -- YOU CAN'T DO THIS.



Indeed, I daresay that if your compiler let you do that, IT IS BROKEN.



This is what you can do:



char* str;



OR, probably maybe what you INTENDED:



typedef char* CharPtr;

CharPtr str;



Either solution is equally acceptable.



Anyhow, that one mistake caused the following errors:



MyString.cpp: In constructor ‘MyString::MyString()’:

MyString.cpp:8: error: ‘str’ was not declared in this scope

MyString.cpp: In constructor ‘MyString::MyString(char*)’:

MyString.cpp:14: error: ‘str’ was not declared in this scope

MyString.cpp: In copy constructor ‘MyString::MyString(const MyString&)’:

MyString.cpp:20: error: ‘str’ was not declared in this scope

MyString.cpp: In destructor ‘MyString::~MyString()’:

MyString.cpp:26: error: ‘str’ was not declared in this scope

MyString.cpp: In member function ‘int MyString::getLen() const’:

MyString.cpp:29: error: ‘str’ was not declared in this scope

MyString.cpp: In member function ‘void MyString::setElement(int, char)’:

MyString.cpp:32: error: ‘str’ was not declared in this scope

MyString.cpp: In member function ‘char MyString::getElement(int) const’:

MyString.cpp:35: error: ‘str’ was not declared in this scope

MyString.cpp: In member function ‘bool MyString::operator==(const MyString&)’:

MyString.cpp:43: error: ‘str’ was not declared in this scope

MyString.cpp: In member function ‘bool MyString::operator!=(const MyString&)’:

MyString.cpp:54: error: ‘str’ was not declared in this scope

MyString.cpp: In member function ‘char& MyString::operator[](int)’:

MyString.cpp:64: error: ‘str’ was not declared in this scope

MyString.cpp: In member function ‘MyString& MyString::operator=(const MyString&)’:

MyString.cpp:72: error: ‘str’ was not declared in this scope

MyString.cpp: In function ‘std::ostream& operator<<(std::ostream&, const MyString&)’:

MyString.cpp:80: error: ‘const class MyString’ has no member named ‘str’

make: *** [MyString] Error 1



Making either change removed ALL of those errors.



There is no "main" function, which I suppose is provided in a separate file that you didn't show us. But I'm amazed you're able to run it. It should NOT have compiled. And it certainly won't run without a "main" function somewhere.
modulo_function
2012-04-14 20:32:19 UTC
You're a 'noob' and you're working with operator overloading? That's not newbie work. Either you're not a newbie or you got this code from someone else.



Ignore that first answerers about a misplaced ;. For a do..while loop:



do {

// code

} while( cond ); // <- the ; belongs there





+add

First semester, eh? In my first, and only, semester of C++ we didn't get anywhere near operator overloading. Heck, we just barely looked at pointers.

Anyways, I plugged this into my Code:;Blocks and didn't get very far. This part of your header:

char* CharPtr;

CharPtr str; // storage for characters in the string. This array will contain a ‘\0’ at

//the end so that C-string functions can be used

int length; //number of characters in str (not counting the ‘\0’)



generated compiler errors: The first one is the one that I'm most confused about. Note that most of the subsequent errors are spurious in that they result from the first. I'd like to help more but I don't know what I can help with. Is: char* CharPtr; CharPtr str; legitimate?



C:\Users\JDWilliams\Documents\CodeBlocks\Juan\MyString.h|14|error: 'CharPtr' does not name a type|

C:\Users\JDWilliams\Documents\CodeBlocks\Juan\JuanApp.cpp||In constructor 'MyString::MyString()':|

C:\Users\JDWilliams\Documents\CodeBlocks\Juan\JuanApp.cpp|9|error: 'str' was not declared in this scope|

C:\Users\JDWilliams\Documents\CodeBlocks\Juan\JuanApp.cpp|11|error: 'strlen' was not declared in this scope|

...deleted text...

||=== Build finished: 17 errors, 0 warnings ===|
Salvatore Rosa
2012-04-14 20:04:53 UTC
I dont know exactly whats wrong , though you do have semi colons ; after your while loops,

ex: while(answer == false);



you shouldnt have a semi colon right after the loop


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...