View Single Post
  #2 (permalink)  
Old 10-04-2006, 06:59 AM
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

Alright, I got my class put together, but I have an insane amount of compiler errors, and I don't get why most of them are there. I mostly followed examples online but for the most part I think it's right. I'm sure some of the errors are easily fixed, however I know I am missing a few major concepts here!
Code:
#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 characters to the end of a string
 friend Cstring operator + (Cstring&);
//this operator copies a string into a result string
 friend Cstring operator = (Cstring&);
//these are of course the input and output operators
 friend Cstring operator >> (Cstring&);
 friend Cstring operator << (Cstring&); 
 //function that returns the first character of the string   
 char head();
 //function that returns the string with the first character removed
 Cstring tail();
};

char Cstring::head()
{
 return array[0];
}

Cstring Cstring::tail()
{
 Cstring temp;
 for(int i=1; i<50; i++;)
 {
  temp.array[i] = array[i];     
 }
 return temp;  
}

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

Cstring::Cstring(char &this)
{
 array[0] = this;
 for (int i=1; i<50; i++;)
 array[i] = ' ';
}

Cstring::Cstring(Cstring &other)
{
 for (int i=0; i<50; i++;)
 array[i] = other.array[i];
}

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

Cstring::operator=(Cstring &left, Cstring &right)
{
 Cstring temp;
 temp.array[i] = right.array[i];
}

ostream&::operator<<(ostream& output, Cstring &right)
{
 for(int i=0; i<50; i++)
 {
  if(right.array[i] != ' ')
  output << right.array[i];
 }
}

istream&::operator>>(istream& input, Cstring &right)
{
 Cstring temp;
 input.getline(temp.array,50);
 return temp;
}

Last edited by hoser2001; 10-04-2006 at 11:02 AM. Reason: thought I should clarify the use of the operators more
Reply With Quote