帮助我的代码?类 [英] Help with my code? Classes
问题描述
我正在尝试编写几个课程。一个叫做PositiveInteger,
应该只存储一个正整数。第二类称为
Circle,用于描述一个圆圈。 Circle用于描述圆的半径时使用
PositiveInteger。对不起,如果没有
正确描述它。
有人可以查看我的代码并告诉我哪里出错了,并且
为什么它一直给我这个错误
错误C2533:''Circle :: Circle'':构造函数不允许返回类型
>
谢谢。
我会发布4个代码文件作为对此消息的回复。
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.
推荐答案
//文件:PositiveInteger.h
#ifndef POSITIVEINTEGER_H
#define POSITIVEINTEGER_H
// - -------------------------------------------------- ---------------
#include< iostream.h>
// - -------------------------------------------------- ----------------
class PositiveInteger
{
public:
/ *构造函数* /
PositiveInteger(); //默认
PositiveInteger(const PositiveInteger& posInt); //复制
~InpositInteger(){}; //析构函数
void Clear(); //清除值为0
/ *设置方法* /
bool SetValue(int);
/ * get方法* /
int GetValue()const {return m_value;}
/ *重载运算符* /
PositiveInteger& operator =(const PositiveInteger& posInt);
bool operator< (const PositiveInteger& posInt);
bool运算符> (const PositiveInteger& posInt);
bool operator ==(const PositiveInteger& posInt);
/ *输入/输出方法* /
朋友ostream&运算符<< (ostream& ostr,const PositiveInteger& posInt);
friend istream&运算符>> (istream& istr,PositiveInteger& posInt);
private:
int m_value;
};
#endif;
// 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;
//文件:PositiveInteger.cpp
#include" PositiveInteger.h"
// -------------------------------- -----------------------------------
PositiveInteger :: PositiveInteger()
{
清除();
}
// --- -------------------------------------------------- --------------
PositiveInteger :: PositiveInteger(const PositiveInteger& posInt)
{
SetValue(posInt.m_value);
}
// ---------------- -------------------------------------------------- -
/ *
PositiveInteger ::〜PositiveInteger()
{
//什么都不做>
}
* /
// ------------------------ -------------------------------------------
>
void PositiveInteger :: Clear()
{
SetValue(0);
}
// -------------------------------------------- -----------------------
bool PositiveInteger :: SetValue(int value)
{
if(value< 0)
{
m_value = 0; //如果是否定的,则设为0
返回false;
}
{
m_value = value;
返回true;
}
}
/ / ------------------------------------------------- ------------------
PositiveInteger& PositiveInteger :: operator =(const PositiveInteger& posInt)
{
if(this!=& posInt)
SetValue(posInt.m_value);
return *这个;
}
// ------------------------- ------------------------------------------
bool PositiveInteger :: operator< (const PositiveInteger& posInt)
{
if(m_value< posInt.m_value)
返回true;
else
返回false;
}
// ------------- -------------------------------------------------- ----
bool PositiveInteger :: operator> (const PositiveInteger& posInt)
{
if(m_value> posInt.m_value)
返回true;
else
返回false;
}
// ------------- -------------------------------------------------- ----
bool PositiveInteger :: operator ==(const PositiveInteger& posInt)
{
if(m_value == posInt.m_value)
返回true;
else
返回false;
}
// ---------------------------------------- ---------------------------
ostream&运算符<< (ostream& ostr,const PositiveInteger& posInt)
{
ostr<< 价值: << posInt.m_value;
返回ostr;
}
// ------------ -------------------------------------------------- -----
istream&运算符<< (istream& istr,PositiveInteger& posInt)
{
int value;
do
{
cout<< 输入一个正整数:;
istr>>价值;
} while(!posInt.SetValue(value));
返回istr;
}
// ---------------------------------------- ---------------------------
// 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::operator = (const PositiveInteger &posInt)
{
if (this != &posInt)
SetValue(posInt.m_value);
return *this;
}
//-------------------------------------------------------------------
bool PositiveInteger::operator < (const PositiveInteger &posInt)
{
if (m_value < posInt.m_value)
return true;
else
return false;
}
//-------------------------------------------------------------------
bool PositiveInteger::operator > (const PositiveInteger &posInt)
{
if (m_value > posInt.m_value)
return true;
else
return false;
}
//-------------------------------------------------------------------
bool PositiveInteger::operator == (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;
}
//-------------------------------------------------------------------
//文件:Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
// ----------- -------------------------------------------------- ------
#include< iostream.h>
#include" PositiveInteger.h"
// -------------------------------------------- -----------------------
class Circle
{
public:
/ * constructors * /
Circle(); //默认
Circle(int radius,int x,int y);
Circle(const Circle& circle); //复制
~Circle(){}; //析构函数
void Clear(); //清除圆圈为0 .....不确定它是如何工作的?
/ *设置方法* /
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(); //如何处理这些?
/ *输入/输出方法* /
//朋友ostream&运算符<< (ostream& ostr,const Circle& circle);
//朋友istream&运算符>> (istream& istr,Circle& circle);
private:
PositiveInteger m_radius;
int m_x;
int m_y;
}
#endif
// 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
这篇关于帮助我的代码?类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!