View Single Post
  #5 (permalink)  
Old 10-04-2006, 01:22 PM
hoser2001's Avatar   
hoser2001 hoser2001 is offline
Programmer
 
Join Date: Jul 2006
Posts: 175
Credits: 0
Rep Power: 11
hoser2001 is on a distinguished road
Default

Ok, I started over and decided to compile little bit at a time instead of cranking stuff out and trying to debug the whole thing... This is how far Ive gotten so far. I commented out the overloading of the = sign because I have no idea whats wrong with that, and I left in the main function because I dont know how to compile it without it, however the rest of it works until I try adding the method for overloading the + operator at which time I get this error message: "..... must take either zero or one argument"
Code:
#include <cstdlib>
#include <iostream>

using namespace std;

class Cstring
{
private:
 //array that will hold the strings
 char array[50];
public: 
 //default constructor
 Cstring();   
 //constructor that turns a single character into a string  
 Cstring(char&);
 //constructor that creates a copy of a string
 Cstring(Cstring&);
 //////////////////////////////////
 //overloaded operators for strings
 //////////////////////////////////
 //this operator adds a single character to the end of a string
 friend Cstring operator + (Cstring&);
 //this operator stores a copy of a string in a result string
 //friend Cstring operator = (Cstring&); //wtf is wrong with this????
 //these are my input/output operators
 friend Cstring operator >> (istream&, Cstring&);
 friend Cstring operator << (ostream&, Cstring&); 
 //function that returns the first character of the string   
 char head();
 //function that returns the string with the first character removed
 Cstring tail();
};

/////////////////////////////////////
//Constructors
////////////////////////////////////

//default constructor
Cstring::Cstring()
{
 for(int i=0; i<50; i++)
 array[i]=' ';
}

//constructor that makes character a string
Cstring::Cstring(char &stuff)
{
 array[0] = stuff;
 for (int i=1; i<50; i++)
 array[i] = ' ';
}

//constructor that copies string to result string
Cstring::Cstring(Cstring &other)
{
 for (int i=0; i<50; i++)
 array[i] = other.array[i];
}

/////////////////////////////////////
//Functions
/////////////////////////////////////

//this is the head function that returns first character of string
char Cstring::head()
{
 return array[0];
}

//this is the tail function that returns the string without the first character
Cstring Cstring::tail()
{
 Cstring temp;
 for(int i=1; i<50; i++)
 {
  temp.array[i] = array[i];     
 }
 return temp;  
}

///////////////////////////////////
//Operator Overloads
///////////////////////////////////

Cstring Cstring::operator+(Cstring &left, char &right)
{
 Cstring temp;
 for(int i=0; i<50; i++)
   temp.array[i] = left.array[i];
         
 for(int j=0; j<50; j++)
 {
  if(temp.array[j]==' ')
  {
   temp.array[j] = right;
   break;
  }
 }
 return temp;           
}





int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}
Reply With Quote

Sponsored Links