Help with my code? Classes

J

J Peterman

I am trying to program a couple classes. One is called PositiveInteger and
is supposed to just store a positive integer. The second class is called
Circle, and is meant to describe a circle. Circle is meant to use
PositiveInteger when describing the circle's radius. Sorry if haven't
described it properly.

Could someone take a look at my code and tell me where I am going wrong, and
why it keeps giving me this error

error C2533: 'Circle::Circle' : constructors not allowed a return type

Thanks.
I'll post the 4 code files as replies to this message.
 
J

J Peterman

// file: PositiveInteger.h

#ifndef POSITIVEINTEGER_H
#define POSITIVEINTEGER_H

//-------------------------------------------------------------------

#include <iostream.h>

//-------------------------------------------------------------------

class PositiveInteger
{
public:
/* constructors */
PositiveInteger(); // default
PositiveInteger(const PositiveInteger &posInt); // copy
~PositiveInteger() {}; // destructor
void Clear(); // clear value to 0

/* set method */
bool SetValue(int);

/* get method */
int GetValue() const {return m_value;}

/* overload operators */
PositiveInteger &operator = (const PositiveInteger &posInt);
bool operator < (const PositiveInteger &posInt);
bool operator > (const PositiveInteger &posInt);
bool operator == (const PositiveInteger &posInt);

/* input/output methods */
friend ostream& operator << (ostream &ostr, const PositiveInteger &posInt);
friend istream& operator >> (istream &istr, PositiveInteger &posInt);

private:
int m_value;
};

#endif;
 
J

J Peterman

// file: PositiveInteger.cpp

#include "PositiveInteger.h"

//-------------------------------------------------------------------

PositiveInteger::positiveInteger()
{
Clear();
}

//-------------------------------------------------------------------

PositiveInteger::positiveInteger(const PositiveInteger &posInt)
{
SetValue(posInt.m_value);
}

//-------------------------------------------------------------------
/*
PositiveInteger::~PositiveInteger()
{
// do nothing
}
*/
//-------------------------------------------------------------------

void PositiveInteger::Clear()
{
SetValue(0);
}

//-------------------------------------------------------------------

bool PositiveInteger::SetValue(int value)
{
if (value < 0)
{
m_value = 0; // if negative, set to 0
return false;
}
else
{
m_value = value;
return true;
}
}

//-------------------------------------------------------------------

PositiveInteger &PositiveInteger::eek:perator = (const PositiveInteger &posInt)
{
if (this != &posInt)
SetValue(posInt.m_value);

return *this;
}

//-------------------------------------------------------------------

bool PositiveInteger::eek:perator < (const PositiveInteger &posInt)
{
if (m_value < posInt.m_value)
return true;
else
return false;
}

//-------------------------------------------------------------------

bool PositiveInteger::eek:perator > (const PositiveInteger &posInt)
{
if (m_value > posInt.m_value)
return true;
else
return false;
}

//-------------------------------------------------------------------

bool PositiveInteger::eek:perator == (const PositiveInteger &posInt)
{
if (m_value == posInt.m_value)
return true;
else
return false;
}

//-------------------------------------------------------------------

ostream& operator << (ostream &ostr, const PositiveInteger &posInt)
{
ostr << "Value: " << posInt.m_value;
return ostr;
}

//-------------------------------------------------------------------

istream& operator << (istream &istr, PositiveInteger &posInt)
{
int value;

do
{
cout << "Enter a positive integer: ";
istr >> value;
}while(!posInt.SetValue(value));
return istr;

}

//-------------------------------------------------------------------
 
J

J Peterman

// file: Circle.h

#ifndef CIRCLE_H
#define CIRCLE_H

//-------------------------------------------------------------------

#include <iostream.h>
#include "PositiveInteger.h"

//-------------------------------------------------------------------

class Circle
{
public:
/* constructors */
Circle(); // default
Circle(int radius, int x, int y);
Circle(const Circle &circle); //copy
~Circle() {}; // destructor
void Clear(); // clear circle to 0.....not sure how it works?

/* set methods */
bool SetRadius(int radius);
bool SetX(int);
bool SetY(int);

/* get methods */
int GetRadius() {return m_radius.GetValue();}
int GetX() const {return m_x;}
int GetY() const {return m_y;}
double GetPerimeter();
double GetArea();//what to do for these?

/* input/output methods */
//friend ostream& operator << (ostream &ostr, const Circle &circle);
//friend istream& operator >> (istream &istr, Circle &circle);

private:
PositiveInteger m_radius;
int m_x;
int m_y;
}

#endif
 
J

J Peterman

// file: Circle.cpp

#include "Circle.h"

//-------------------------------------------------------------------

Circle::Circle()
{
Clear();
}

//-------------------------------------------------------------------

Circle::Circle(int radius, int x, int y)
{
SetRadius(radius);
SetX(x);
SetY(y);
}

//-------------------------------------------------------------------

Circle::Circle(const Circle &circle)
{
SetRadius(circle.m_radius.GetValue());
SetX(circle.m_x);
SetY(circle.m_y);
}

//-------------------------------------------------------------------

void Circle::Clear()
{
m_radius.SetValue(0);
SetRadius(0);
SetX(0);
SetY(0);
}

//-------------------------------------------------------------------

bool Circle::SetRadius(int radius)
{
if (m_radius.SetValue(radius))
{
m_radius.SetValue(radius);
return true;
}
else
return false;
}

//-------------------------------------------------------------------

bool Circle::SetX(int x)
{
m_x = x;
return true;
}

//-------------------------------------------------------------------

bool Circle::SetY(int y)
{
m_y = y;
return true;
}

//-------------------------------------------------------------------
 
K

Karl Heinz Buchegger

J said:
I am trying to program a couple classes. One is called PositiveInteger and
is supposed to just store a positive integer. The second class is called
Circle, and is meant to describe a circle. Circle is meant to use
PositiveInteger when describing the circle's radius. Sorry if haven't
described it properly.

Could someone take a look at my code and tell me where I am going wrong, and
why it keeps giving me this error

error C2533: 'Circle::Circle' : constructors not allowed a return type

It would help if you indicate *which* file gave that error.

But loook in Circle.h
You lost the ';' to end the class declaration

class Circle
{
....
} ;

^
|
missing
 
S

Sam Holden

class Circle
{ [snip public section]

private:
PositiveInteger m_radius;
int m_x;
int m_y;
}
^
A character is missing here. The PositiveInteger.h has that character,
so it shouldn't be hard to work out :)
 
J

J Peterman

Thanks a lot!
I knew it would be something stupid like that. I just couldn't see it...
 
P

Patrick Kowalzick

Hi all,

wasn't the destructor implemented inline inside the class?

Should produce a compile error, or not?

Reagrds,
Patrick
 
J

J Peterman

Sorry, I am not exactly sure what I am doing...still new to this. If I'm not
mistaken, the destructor is commented cos I wasn't sure if I needed it or
not..
 
J

jeffc

J Peterman said:
Thanks a lot!
I knew it would be something stupid like that. I just couldn't see it...

That is a tough error to find as the compiler message will always be
misleading to you. But, after some experience, you'll be able to spot this
error even based on funny error messages.
 
M

Mike Wahler

jeffc said:
That is a tough error to find as the compiler message will always be
misleading to you. But, after some experience, you'll be able to spot this
error even based on funny error messages.

Even better, develop coding habits that prevent this from
happening at all. E.g. when you define a class, write the
entire 'skeleton' first, e.g.

class MyClass
{
};

Then go back and fill it in.

Much easier to visually verify that all the syntax is
correct than when your class gets full of declarations, etc.

-Mike
 
J

J Peterman

Even better, develop coding habits that prevent this from
happening at all. E.g. when you define a class, write the
entire 'skeleton' first, e.g.

class MyClass
{
};

Then go back and fill it in.

Much easier to visually verify that all the syntax is
correct than when your class gets full of declarations, etc.

-Mike

Yeah, I started doing that! Thanks!
 
P

Patrick Kowalzick

//-------------------------------------------------------------------
oh, sorry, overseen the comment. I miss syntax-highligthing for my
newsreader ;-).

Patrick
 
K

Kevin Goodsell

J said:
Yeah, I started doing that! Thanks!

Also, always look *very closely* at the 2 or 3 lines ABOVE the line an
error is reported on. Sometimes those lines are not in the same file,
though.

-Kevin
 
J

jeffc

Kevin Goodsell said:
Also, always look *very closely* at the 2 or 3 lines ABOVE the line an
error is reported on. Sometimes those lines are not in the same file,
though.

Key point - that's why that one's so tricky.
 

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

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top