Thread: class my_string woes

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    31

    class my_string woes

    I can't figure out what's wrong with my + function. 'my_string temp' doesn't appear to be assigning a memory address to temp.s or the correct value to temp.len and so the terminates with an error. Please help.

    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    class my_string {
    	char *s;
    	int len;
    public:
    	my_string(int n) { s = new char[n+1]; len = n; }
    	my_string() { len = 255; s = new char[255]; }
    	~my_string() { delete s; }
    	void assign(char* st) { strcpy(s, st); len = strlen(st); }
    	int length() { return (len); }
    	void print() { cout << s << "\nLength: " << len << "\n"; }
    	friend my_string operator +(my_string& a, my_string& b);
    };
    
    main()
    {
    	char achar[] = "Yay!", bchar[] = " Coolio!";
    
    	my_string a, b, c;
    
    	a.assign(achar), b.assign(bchar);
    
    	c = a+b;
    }
    
    my_string operator +(my_string& a, my_string& b)
    {
    	my_string temp;
    
    	temp.assign(strcpy(temp.s, a.s));
    	if (a.len + b.len < 255)
    		temp.assign(strcat(temp.s, b.s));
    	else
    		cerr << "Max length exceeded in concatenation.\n";
    	return (temp);
    }
    
    void print(char *c)
    {
    	cout << c << "\nLength is " << strlen(c) << "\n";
    }
    Last edited by Kayl669; 04-11-2011 at 06:21 AM.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Problem resolved. Resolution: inadequate specification of constructors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MD2 woes
    By psychopath in forum Game Programming
    Replies: 9
    Last Post: 07-02-2005, 07:46 PM
  2. Exception Handling Class Woes
    By ventolin in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2005, 08:37 PM
  3. ASP.NET woes
    By nickname_changed in forum C# Programming
    Replies: 0
    Last Post: 03-20-2004, 04:34 PM
  4. Dll woes
    By Abiotic in forum Windows Programming
    Replies: 3
    Last Post: 11-09-2003, 11:32 AM
  5. Win32 class woes
    By xds4lx in forum Windows Programming
    Replies: 2
    Last Post: 02-22-2002, 05:16 PM