473,881 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New to C++ ; trying to create a class

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::Cstrin g()
{
for(int i=0; i<50; i++;)
array[i]=' ';
}

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

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

Cstring Cstring::operat or+(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::operat or=(Cstring &left, Cstring &right)
{
Cstring temp;
temp.array[i] = right.array[i];
}

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

istream&::opera tor>>(istream& input, Cstring &right)
{
Cstring temp;
input.getline(t emp.array,50);
return temp;
}

Oct 4 '06 #1
4 1735
rh****@gmail.co m 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
<rh****@gmail.c omwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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
rh****@gmail.co m 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
<rh****@gmail.c omwrote:
>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(t emp.array,50);
return temp;
}
#endif
Oct 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2746
by: Dmitry Tkach | last post by:
Hi, everybody! I am trying to create a custom GiST index in 7.3, but getting an error, that I don't know how to interpret: testdb=# create table gist_test (field int8); CREATE TABLE testdb=# create index gist_idx on gist_test using gist (field); ERROR: data type bigint has no default operator class for access method "gist"
2
1329
by: Dave | last post by:
I know in C++ you can create class templates so the data type can be defined during the declaration of an instance of a class. I was trying to do something like that for a VB.NET class cut am not really sure where to start. Here is an example for a simple DataArray class definition that sets up an array used for numerical data and adds some additional functionality to the array. The overloaded new methods work okay, although I'm not sure...
1
1007
by: Rob R. Ainscough | last post by:
I'm trying to figure out how Microsoft were able to setup the namespace/assemblies in such a way that the corresponding Class Library DLL's seem to be aware of the other Parent/child DLL relationship? i.e. System (is a System.DLL), System.Data (is System.Data.DLL), etc. etc. when I try to create my own such structure I can't seem to get it to work. I create an assembly (new project based on class library) say call "MyCompany", build...
5
3660
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As New System.Diagnostics.Process 'p.Start(MDEPDirStr & "macrun.exe", sPPTOut) p.Start("C:\WINDOWS\SYSTEM32\CALC.EXE") 'p.Start("C:\WINDOWS\SYSTEM32\macrun.exe", sPPTOut)
5
3797
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public List<RoleData> GetRoles() { return GetRoles(null, false); }
0
1118
by: Jiho Han | last post by:
I have a web method that accepts an abstract class. The abstract class includes an XmlIncludeAttribute that specifies derived classes. Wsdl gets generated fine that includes both schema for the abstract class and derived classes mention in XmlIncludeAttribute list. The trouble is when a call is made to the method such as: public void Update(AbstractEntity entity) { }
20
1914
by: walterbyrd | last post by:
Reading "Think Like a Computer Scientist" I am not sure I understand the way it describes the way objects work with Python. 1) Can attributes can added just anywhere? I create an object called point, then I can add attributes any time, and at any place in the program? 2) Are classes typically created like this: class Point:
6
3366
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in the past in other places, and while the help was much appreciated, it seemed everyone just wanted to 'theoretically' explain how to do it, but when I tried to do it myself, I couldn't login. I want to simply pass the email address and password to...
0
3238
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have given all the attributes which are needed, for the user, in the code and also the proper path where the user has to be added. Please have a look at my code CODE] // This is a class file which stores all the info required for the user
4
2334
by: =?Utf-8?B?R29yZnk=?= | last post by:
Hey everyone. This is only my first week with C#, but I use actionscript a lot in Flash, so I have a very basic understanding of programming, but bear with me... I am trying to create a "FadeClass" that I can use to fade windows in my programs. I am using Visual C# 2008 Express right now. This is my "Form1.cs" in it's current state: using System;
0
9926
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11096
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10716
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10812
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10400
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7953
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7108
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.