error in user defined string class

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2005
Posts: 11
Reputation: supersonic is an unknown quantity at this point 
Solved Threads: 0
supersonic supersonic is offline Offline
Newbie Poster

error in user defined string class

 
-1
  #1
Feb 1st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: error in user defined string class

 
0
  #2
Feb 1st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: error in user defined string class

 
0
  #3
Feb 1st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 11
Reputation: supersonic is an unknown quantity at this point 
Solved Threads: 0
supersonic supersonic is offline Offline
Newbie Poster

Re: error in user defined string class

 
0
  #4
Feb 1st, 2005
ok here it is with the code tags. sorry

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class String {
  6. char *str; //pointer to character block
  7. public:
  8. String();
  9. String (char *s);
  10. void setString (char *s);
  11. int stringLength();
  12. char getString();
  13.  
  14.  
  15. };
  16. String::String() {}
  17.  
  18.  
  19. String::String(char *s)
  20. {
  21. int length=strlen(s); //length of string
  22. str=new char[length+1]; //increments length of string
  23. strcpy(str,s); //copies s to str
  24. cout<<str<<'\n';
  25. }
  26.  
  27. void String::setString(char *s)
  28. {
  29. int length=strlen(s); //length of string
  30. str=new char[length+1]; //increments length of string
  31. strcpy(str,s); //copies s to str
  32. cout<<str<<'\n';
  33. }
  34.  
  35. int String::stringLength()
  36. {
  37. int length=strlen(str);;
  38.  
  39. return length;
  40. }
  41.  
  42. char String:: getString()
  43. {
  44. char *s;
  45. int length=strlen(s); //length of string
  46. str=new char[length+1]; //increments length of string
  47. strcpy(str,s); //copies s to str
  48. cout<<str<<'\n';
  49. return *str;
  50. }
  51.  
  52.  
  53.  
  54. int main()
  55. {
  56. String s1="Hello World";
  57. String s2;
  58.  
  59. s2.setString("THis is a string");
  60. cout<<"string 1 is "<<s1.stringLength()<<"characters long"<< endl;
  61. cout << "string 1 is " << s1.getString()<< endl;
  62.  
  63.  
  64. return 0;
  65. }

Thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 11
Reputation: supersonic is an unknown quantity at this point 
Solved Threads: 0
supersonic supersonic is offline Offline
Newbie Poster

Re: error in user defined string class

 
0
  #5
Feb 1st, 2005
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).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3003 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC