主题:[讨论]建一个类的问题,大家帮我看看为什么报错。。。多谢!!!
关于建一个类的问题,求助!!
我第一次建类。。。
要建一个半整数类,就是说只存在n/2, n是整数。下面是我的类:
#include <iostream>
#include <cmath>
#include <string>
class halfint{
private:
int numerator;
int denominator;
public:
//CONSTRUCTORS
halfint (int, int);
halfint (int);
//COPY CONSTRUCTOR
halfint (halfint&);
//EXAMINE OBJECTS
void display();
//OPERATIONS
void operator = (halfint);
void set(halfint);
halfint operator + (halfint);
void operator += (halfint);
halfint operator - (halfint);
bool operator >= (halfint);
halfint invert (halfint);
int gettwice (halfint);
};
//CONSTRUCTORS
halfint :: halfint (int n, int d){
numerator = n;
denominator = d;
}
halfint :: halfint (int whole){
numerator = whole * 2;
denominator = 2;
}
void halfint :: set(halfint h){
this->numerator = h.numerator;
this->denominator = h.denominator;
}
//COPY CONSTRUCTOR
halfint :: halfint (halfint &h){
this->numerator = h.numerator;
this->denominator = h.denominator;
}
void halfint :: display(){
if (this->numerator % 2 == 0){
this->numerator /= 2;
this->denominator /= 2;
std::cout << this->numerator <<"\n";
}
else{
std::cout << this->numerator << "/" << this->denominator<<"\n";
}
}
void halfint :: operator = (halfint h){
this->numerator = h.numerator;
//this->
this->denominator = h.denominator;
}
halfint halfint :: operator + (halfint h){
halfint sum(0);
sum.numerator = this->numerator + h.numerator;
return sum;
}
halfint halfint :: operator - (halfint h){
halfint diff(0);
diff.numerator = this->numerator - h.numerator;
return diff;
}
bool halfint :: operator >= (halfint h){
bool grteq;
halfint dummy(0);
halfint dummyh(0);
dummy.numerator = this->numerator;
dummyh.numerator = h.numerator;
if (dummy.numerator >= dummyh.numerator) {
grteq = true;
}
else {
grteq = false;
}
return grteq;
}
halfint halfint :: invert (halfint h){
if (h.numerator < 0) {
this->numerator = (-1)*h.numerator;
};
return *this;
}
halfint absolute (halfint h){
halfint zero(0);
halfint dummy(0);
if (zero >= h) {
dummy.invert(h);
}
return dummy;
}
halfint maximum (halfint h1, halfint h2){
if (h1 >= h2) {
return h1;
}
else {
return h2;
}
}
void halfint :: operator += (halfint h){
this->numerator += h.numerator;
}
int halfint :: gettwice (halfint h){
return h.numerator;
}
int getnum (halfint dummy){
int n;
n = dummy.gettwice(dummy);
return n;
}
halfint assign (int num, int denom){
halfint h1(num, denom);
return h1;
下面是我的main:
#include <iostream>
#include <cmath>
#include <string>
#include "halfint.h"
using namespace std;
int main (int argc, char * const argv[]) {
halfint a(-3, 2);
halfint b(8);
halfint c(0);
(a+b).display();
maximum(a,b).display;
c,set(a);
//从这里开始都报错。。。貌似所有return的值都不能传递到我的object上
c=a;
c = a+b;
c.set(a+b);
c = maximum(a,b);
return 0;
}
有人知道怎么办吗??谢谢啦!
我第一次建类。。。
要建一个半整数类,就是说只存在n/2, n是整数。下面是我的类:
#include <iostream>
#include <cmath>
#include <string>
class halfint{
private:
int numerator;
int denominator;
public:
//CONSTRUCTORS
halfint (int, int);
halfint (int);
//COPY CONSTRUCTOR
halfint (halfint&);
//EXAMINE OBJECTS
void display();
//OPERATIONS
void operator = (halfint);
void set(halfint);
halfint operator + (halfint);
void operator += (halfint);
halfint operator - (halfint);
bool operator >= (halfint);
halfint invert (halfint);
int gettwice (halfint);
};
//CONSTRUCTORS
halfint :: halfint (int n, int d){
numerator = n;
denominator = d;
}
halfint :: halfint (int whole){
numerator = whole * 2;
denominator = 2;
}
void halfint :: set(halfint h){
this->numerator = h.numerator;
this->denominator = h.denominator;
}
//COPY CONSTRUCTOR
halfint :: halfint (halfint &h){
this->numerator = h.numerator;
this->denominator = h.denominator;
}
void halfint :: display(){
if (this->numerator % 2 == 0){
this->numerator /= 2;
this->denominator /= 2;
std::cout << this->numerator <<"\n";
}
else{
std::cout << this->numerator << "/" << this->denominator<<"\n";
}
}
void halfint :: operator = (halfint h){
this->numerator = h.numerator;
//this->
this->denominator = h.denominator;
}
halfint halfint :: operator + (halfint h){
halfint sum(0);
sum.numerator = this->numerator + h.numerator;
return sum;
}
halfint halfint :: operator - (halfint h){
halfint diff(0);
diff.numerator = this->numerator - h.numerator;
return diff;
}
bool halfint :: operator >= (halfint h){
bool grteq;
halfint dummy(0);
halfint dummyh(0);
dummy.numerator = this->numerator;
dummyh.numerator = h.numerator;
if (dummy.numerator >= dummyh.numerator) {
grteq = true;
}
else {
grteq = false;
}
return grteq;
}
halfint halfint :: invert (halfint h){
if (h.numerator < 0) {
this->numerator = (-1)*h.numerator;
};
return *this;
}
halfint absolute (halfint h){
halfint zero(0);
halfint dummy(0);
if (zero >= h) {
dummy.invert(h);
}
return dummy;
}
halfint maximum (halfint h1, halfint h2){
if (h1 >= h2) {
return h1;
}
else {
return h2;
}
}
void halfint :: operator += (halfint h){
this->numerator += h.numerator;
}
int halfint :: gettwice (halfint h){
return h.numerator;
}
int getnum (halfint dummy){
int n;
n = dummy.gettwice(dummy);
return n;
}
halfint assign (int num, int denom){
halfint h1(num, denom);
return h1;
下面是我的main:
#include <iostream>
#include <cmath>
#include <string>
#include "halfint.h"
using namespace std;
int main (int argc, char * const argv[]) {
halfint a(-3, 2);
halfint b(8);
halfint c(0);
(a+b).display();
maximum(a,b).display;
c,set(a);
//从这里开始都报错。。。貌似所有return的值都不能传递到我的object上
c=a;
c = a+b;
c.set(a+b);
c = maximum(a,b);
return 0;
}
有人知道怎么办吗??谢谢啦!