New to C++ ; trying to create a class

R

rhaazy

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 = array;
}
return temp;
}

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

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

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

Cstring Cstring::eek:perator+(Cstring &left, char &right)
{
Cstring temp;
for(int i=0; i<50; i++)
temp.array = left.array;

for(i=0; i<50; i++)
{
if(temp.array==' ')
{
temp.array = right;
break
}
}
return temp;
}

Cstring::eek:perator=(Cstring &left, Cstring &right)
{
Cstring temp;
temp.array = right.array;
}

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

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

mlimber

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-lite/containers.html#faq-34.1

Cheers! --M
 
J

John Carson

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.
 
M

mlimber

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-lite/how-to-learn-cpp.html#faq-28.4

Cheers! --M
 
O

osmium

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

Cstring temp;
input.getline(temp.array,50);
return temp;
}
#endif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top