|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
| Sponsored Links |
|
|
|
|||||
|
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 |
|
|||||
|
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;
}
|
| Sponsored Links |
|
|
|
|||||
|
Alright, I finally got my class to compile, and I'm pretty sure everything is alright, but now I have to use this class in another program in which I perform a couple of recursive operations. RECURSION PWNS ME....
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 left, const char right);
//this operator stores a copy of a string in a result string
Cstring Cstring::operator = (Cstring& right);
//these are my input/output operators
friend istream& operator >> (istream& input, Cstring& right);
friend ostream& operator << (ostream& output, Cstring& right);
//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 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;
}
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);
input >> temp;
}
Cstring Cstring::operator=(Cstring& right)
{
int i=0;
Cstring temp;
temp.array[i] = right.array[i];
return temp;
}
1.)a function that returns the reverse of a string 2.)a function that concatenates the reverse of a string onto the original string 3.)a function that tests if a certain string is the prefix of another string If anyone can point me in some possible dirrection, or even try to explain to me exactly how it is recursion works I would appreciate the help greatly!!!! |
|
|||
|
That is a neat function. I'm not sure I will get the syntax right here but couldn't you use this to return the reverse of a string: (I'm also not sure of the array functions)
Code:
array[0] = array;
rArray[0] = array;
int count=0;
for (int i=length(array); i>=0; i--)
{
rArray[count] = array[i];
count++;
}
|
|
|||||
|
You have most of what you need for doing a reverse.
The basic idea of recursion is to break a task into many smaller steps that are identical. For instance, if you want to compute a factorial (5! = 5*4*3*2*1), one way to do it is to say n! = n*(n-1)!, and note that 0!=1. For reversing your string, just note that MyString.rev() = MyString.tail().rev()+MyString.head(), and a single character string is its own reverse. Once you have part 1 done, part 2 is trivial. For testing the prefix, just test the heads and tails of the strings.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
|
|||||
|
It's a pretty standard programming project for classes.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Issue writing to file: pointer to a class which contains pointers to other classes | Sheemer | C and C++ | 0 | 08-21-2007 02:17 AM |
| Class question in Delphi | tosh5457 | Pascal/Delphi | 4 | 06-11-2007 02:46 PM |
| null exception problem | connor7777 | C# Programming | 2 | 03-28-2007 12:37 PM |
| Creating a Custom Cursor | ahsan16 | Tutorials | 2 | 01-13-2007 06:03 PM |
| Java Help Files | xXHalfSliceXx | Java Help | 3 | 11-29-2006 12:30 AM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |