954,167 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

error in user defined string class

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

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<

supersonic
Newbie Poster
11 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

Please use Code tags when posting code it makes helping much better.

prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 

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;
  
}
prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 

ok here it is with the code tags. sorry

#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

supersonic
Newbie Poster
11 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

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

supersonic
Newbie Poster
11 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You