944,198 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3627
  • C++ RSS
Feb 1st, 2005
-1

error in user defined string class

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
supersonic is offline Offline
11 posts
since Feb 2005
Feb 1st, 2005
0

Re: error in user defined string class

Please use Code tags when posting code it makes helping much better.
Reputation Points: 14
Solved Threads: 4
Junior Poster
prog-bman is offline Offline
108 posts
since Nov 2004
Feb 1st, 2005
0

Re: error in user defined string class

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;
  
}
Reputation Points: 14
Solved Threads: 4
Junior Poster
prog-bman is offline Offline
108 posts
since Nov 2004
Feb 1st, 2005
0

Re: error in user defined string class

ok here it is with the code tags. sorry

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
supersonic is offline Offline
11 posts
since Feb 2005
Feb 1st, 2005
0

Re: error in user defined string class

Quote 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).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
supersonic is offline Offline
11 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: In over my head
Next Thread in C++ Forum Timeline: Novice asking for help in Learning C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC