| | |
error in user defined string class
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2005
Posts: 11
Reputation:
Solved Threads: 0
I almost got my user defined string class to work but not quite. When I execute the program, I get a memory error. Code is as follows.
#include <iostream>
using namespace std;
class String {
char *str; //pointer to character block
public:
String();
String (char *s);
void setString (char *s);
int stringLength();
char getString();
};
String:
tring() {}
String:
tring(char *s)
{
int length=strlen(s); //length of string
str=new char[length+1]; //increments length of string
strcpy(str,s); //copies s to str
cout<<str<<'\n';
}
void String::setString(char *s)
{
int length=strlen(s); //length of string
str=new char[length+1]; //increments length of string
strcpy(str,s); //copies s to str
cout<<str<<'\n';
}
int String::stringLength()
{
int length=strlen(str);;
return length;
}
char String:: getString()
{
char *s;
int length=strlen(s); //length of string
str=new char[length+1]; //increments length of string
strcpy(str,s); //copies s to str
cout<<str<<'\n';
return *str;
}
int main()
{
String s1="Hello World";
String s2;
s2.setString("THis is a string");
cout<<"string 1 is "<<s1.stringLength()<<"characters long"<< endl;
cout << "string 1 is " << s1.getString()<< endl;
return 0;
}
I think it has to do with my getstring() function. The reason I have return returning str is because it should be returning my string as shown in the main program. But perhaps I have overlooked something.
Error I get when executed is that memory could not be read.
Thanks in advance.
#include <iostream>
using namespace std;
class String {
char *str; //pointer to character block
public:
String();
String (char *s);
void setString (char *s);
int stringLength();
char getString();
};
String:
tring() {}String:
tring(char *s){
int length=strlen(s); //length of string
str=new char[length+1]; //increments length of string
strcpy(str,s); //copies s to str
cout<<str<<'\n';
}
void String::setString(char *s)
{
int length=strlen(s); //length of string
str=new char[length+1]; //increments length of string
strcpy(str,s); //copies s to str
cout<<str<<'\n';
}
int String::stringLength()
{
int length=strlen(str);;
return length;
}
char String:: getString()
{
char *s;
int length=strlen(s); //length of string
str=new char[length+1]; //increments length of string
strcpy(str,s); //copies s to str
cout<<str<<'\n';
return *str;
}
int main()
{
String s1="Hello World";
String s2;
s2.setString("THis is a string");
cout<<"string 1 is "<<s1.stringLength()<<"characters long"<< endl;
cout << "string 1 is " << s1.getString()<< endl;
return 0;
}
I think it has to do with my getstring() function. The reason I have return returning str is because it should be returning my string as shown in the main program. But perhaps I have overlooked something.
Error I get when executed is that memory could not be read.
Thanks in advance.
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
Please use Code tags when posting code it makes helping much better.
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
Here's a little string class I wrote. I am not sure how good it is but it works 

#include <iostream> namespace myString { class string { public: string(); string(char *s); void operator =(char *s); int length(); char *getString(); private: char *str; }; string::string() { } string::string(char *s) { str = new char[sizeof(s)]; str = s; } void string::operator =(char *s) { str = new char[sizeof(s)]; str = s; } int string::length() { return (sizeof(str) + 1); } char *string::getString() { return str; } } int main() { myString::string mystring; mystring = "Hello"; std::cout<<mystring.getString()<<std::endl; std::cin.get(); return 0; }
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
•
•
Join Date: Feb 2005
Posts: 11
Reputation:
Solved Threads: 0
ok here it is with the code tags. sorry
Thanks
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class String { char *str; //pointer to character block public: String(); String (char *s); void setString (char *s); int stringLength(); char getString(); }; String::String() {} String::String(char *s) { int length=strlen(s); //length of string str=new char[length+1]; //increments length of string strcpy(str,s); //copies s to str cout<<str<<'\n'; } void String::setString(char *s) { int length=strlen(s); //length of string str=new char[length+1]; //increments length of string strcpy(str,s); //copies s to str cout<<str<<'\n'; } int String::stringLength() { int length=strlen(str);; return length; } char String:: getString() { char *s; int length=strlen(s); //length of string str=new char[length+1]; //increments length of string strcpy(str,s); //copies s to str cout<<str<<'\n'; return *str; } int main() { String s1="Hello World"; String s2; s2.setString("THis is a string"); cout<<"string 1 is "<<s1.stringLength()<<"characters long"<< endl; cout << "string 1 is " << s1.getString()<< endl; return 0; }
Thanks
•
•
Join Date: Feb 2005
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by prog-bman
Here's a little string class I wrote. I am not sure how good it is but it works
#include <iostream> namespace myString { class string { public: string(); string(char *s); void operator =(char *s); int length(); char *getString(); private: char *str; }; string::string() { } string::string(char *s) { str = new char[sizeof(s)]; str = s; } void string::operator =(char *s) { str = new char[sizeof(s)]; str = s; } int string::length() { return (sizeof(str) + 1); } char *string::getString() { return str; } } int main() { myString::string mystring; mystring = "Hello"; std::cout<<mystring.getString()<<std::endl; std::cin.get(); return 0; }
Thanks. I executed it and did notget any errors which is a good thing
ANd you had some differnet ideas that I will look at when I get to work and have more time in front of my laptop (hopefully). ![]() |
Similar Threads
- An idictionary of user-defined class (C++)
- String Class (C++)
- wont let me create a vector of my user defined class (C++)
- Undefined reference error (C++)
- DECLARATION SYNTAX ERROR (for bc 31 user) (C++)
Other Threads in the C++ Forum
- Previous Thread: In over my head
- Next Thread: Novice asking for help in Learning C++
Views: 3003 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





