By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

New to C++ ; trying to create a class

P: n/a
I have created this very primitive class for the purpose of performing
some simple recursive functions. I am new to C++ and the concept of
classes all together. I put this together using various resources on
the net, but am faced with an insane amount of compiler errors I don't
know what to do with. Any help would be greatly appreciated.

#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&);
//these are my input/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;
}

Oct 4 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
rh****@gmail.com wrote:
I have created this very primitive class for the purpose of performing
some simple recursive functions. I am new to C++ and the concept of
classes all together. I put this together using various resources on
the net, but am faced with an insane amount of compiler errors I don't
know what to do with. Any help would be greatly appreciated.
[snip code using arrays]

First things first: use std::string (or at least std::vector), not
arrays. See

http://www.parashift.com/c++-faq-lit....html#faq-34.1

Cheers! --M

Oct 4 '06 #2

P: n/a
<rh****@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com
I have created this very primitive class for the purpose of performing
some simple recursive functions. I am new to C++ and the concept of
classes all together. I put this together using various resources on
the net, but am faced with an insane amount of compiler errors I don't
know what to do with. Any help would be greatly appreciated.
I suggest you start with something simpler first. If you write a lot of code
and don't know what you are doing, then you do get an "insane" amount of
compiler errors. Try a single member function, then a single operator. Get
those to compile. Then add a small amount of code and compile again.

It is not clear how many of your errors are conceptual and how many are the
result of sloppy typing. If there is too much code, then it is just too much
trouble to try to figure it out and then point it out.

Four points:

1. A class's own member functions/operators are never friends.
2. You need to have the same number and type of parameters (and in the same
order) in your function declaration and your function definition.
3. If a function is declared as returning a particular type, then it must
return that type.
4. Never use the name this for a variable. this is predefined as the name of
a pointer to a class's members.

--
John Carson
Oct 4 '06 #3

P: n/a
rh****@gmail.com wrote:
I have created this very primitive class for the purpose of performing
some simple recursive functions. I am new to C++ and the concept of
classes all together. I put this together using various resources on
the net, but am faced with an insane amount of compiler errors I don't
know what to do with. Any help would be greatly appreciated.
PS, I'd suggest you get a good book rather than trying to cobble things
together from the net (well, unless you're using _Thinking in C++_ by
Eckel, which is a good book that is also available for free on the
net). IMHO, the *best* book for learning the right way by example from
the ground up is _Accelerated C++_ by Koenig and Moo. See this FAQ and
the others in this section for more suggestions on learning C++:

http://www.parashift.com/c++-faq-lit....html#faq-28.4

Cheers! --M

Oct 4 '06 #4

P: n/a
<rh****@gmail.comwrote:
>I have created this very primitive class for the purpose of performing
some simple recursive functions. I am new to C++ and the concept of
classes all together. I put this together using various resources on
the net, but am faced with an insane amount of compiler errors I don't
know what to do with. Any help would be greatly appreciated.
You can "parse out" huge chunks of code with #if 0 < ...#endif pairs.
For example put an #if 0 after the close of the class definition. There is
no point in fixing syntax errors in a member function that can't even be
used after all that time invested in getting the detailed syntax right. You
can also use // to cause smaller pieces of code to be ignored.

I think your basic problem is you are trying to start at too high a level.
>
#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&);
//these are my input/output operators
friend Cstring operator >(Cstring&);
There is no implicit argument for friend functions. So you must specify two
arguments.
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();
};
#if 0

<snip>
Cstring temp;
input.getline(temp.array,50);
return temp;
}
#endif
Oct 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.