注册 登录
编程论坛 VC++/MFC

求解答重载运算符编译为什么不通过

printf0 发布于 2010-12-09 14:20, 1244 次点击
// gg.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <iostream>
using namespace std;

class Shape
{public:
    virtual double area() const {return 0.0;}  
    virtual double max() const {return 0.0;}  
    virtual void shapeName() const =0;            
};
class Trapezoid;
class circle:public Shape
{
public:
    circle(double=0,double=0,double=0);
    setcircle(double,double,double);
    void Judgement(circle &p);
    virtual double area();
    virtual void shapeName() const {cout<<"circle:";}
    operator + (Trapezoid &t);
    operator + (circle &c);
private:
    double x;
    double y;
    double ra;
};
circle::setcircle(double a,double b,double r)
{ x=a;y=b;ra=r;}
circle::circle(double a,double b,double r)
{x=a;y=b;ra=r;}
void circle::Judgement(circle &c)
{if (ra>=0)
cout<<"是个圆"<<endl;
}
circle::operator + (Trapezoid &t)
{ return circle(x+t.getx(),y+t.gety(),ra+t.max());}

double circle::area()
{cout<<"area="<<ra*ra*3.1415<<endl;
return 0;}

class Trapezoid:public circle
{
    Trapezoid(double=0,double=0,double=0);
    getx();
    gety();
    virtual double max() const;
    void Judgement(Trapezoid &t);
private:
    double top;
    double bot;
    double hig;
};
Trapezoid::getx()
{return top;}
Trapezoid::gety()
{return bot;}
double Trapezoid::max () const   
{
    double m,n;
    m=top>bot?top:bot;   
    n=hig>m?hig:m;        
    return n;
}
void Trapezoid::Judgement(Trapezoid &t)
{
    if(top>0 && bot>0 && hig>0)
        cout<<"是一个梯形!"<<endl;
    else
        cout<<"不是一个梯形!"<<endl;
}

Trapezoid::Trapezoid(double a,double b,double h)
{top=a;bot=b;hig=h;}

int main(int argc, char* argv[])
{
    printf("Hello World!\n");
    return 0;
}

错误提示是
--------------------Configuration: gg1 - Win32 Debug--------------------
Compiling...
gg1.cpp
c:\users\me\desktop\gg1.cpp(39) : error C2027: use of undefined type 'Trapezoid'
        c:\users\me\desktop\gg1.cpp(14) : see declaration of 'Trapezoid'
c:\users\me\desktop\gg1.cpp(39) : error C2228: left of '.getx' must have class/struct/union type
c:\users\me\desktop\gg1.cpp(39) : error C2027: use of undefined type 'Trapezoid'
        c:\users\me\desktop\gg1.cpp(14) : see declaration of 'Trapezoid'
c:\users\me\desktop\gg1.cpp(39) : error C2228: left of '.gety' must have class/struct/union type
c:\users\me\desktop\gg1.cpp(39) : error C2027: use of undefined type 'Trapezoid'
        c:\users\me\desktop\gg1.cpp(14) : see declaration of 'Trapezoid'
c:\users\me\desktop\gg1.cpp(39) : error C2228: left of '.max' must have class/struct/union type
执行 cl.exe 时出错.

gg1.exe - 1 error(s), 0 warning(s)
10 回复
#2
laoyang1032010-12-09 15:20
operator + (Trapezoid &t);
    operator + (circle &c);
没有返回值   至少要加个看你的代码应该是返回本类对象的引用
#3
printf02010-12-09 16:36
circle::operator + (Trapezoid &t)
{ return circle(x+t.getx(),y+t.gety(),ra+t.max());}

有返回值啊
#4
printf02010-12-09 17:13
提示是用力没有定义的Trapezoid。。。啊不解
#5
laoyang1032010-12-09 21:55
circle::这是类标识
#6
printf02010-12-09 23:12
回复 5楼 laoyang103
额 还是没有理解~~能不能把错的地方纠正一下写出来 谢谢了
#7
laoyang1032010-12-10 10:25
你的那个运算符+ return后面是调用了一个构造函数  
返回了一个对象   所以你的返回值应该是本类的对象
circle &circle::operator + (Trapezoid &t)
{ return circle(x+t.getx(),y+t.gety(),ra+t.max());}
返回引用

#8
printf02010-12-10 12:44
额 重载运算符需要这样定义?~~~
#9
printf02010-12-10 12:51
回复 2楼 laoyang103
额 修改过之后
--------------------Configuration: gg1 - Win32 Debug--------------------
Compiling...
gg1.cpp
C:\Users\me\Desktop\gg1.cpp(39) : error C2556: 'class circle &__thiscall circle::operator +(class Trapezoid &)' : overloaded function differs only by return type from 'int __thiscall circle::operator +(class Trapezoid &)'
        C:\Users\me\Desktop\gg1.cpp(23) : see declaration of '+'
C:\Users\me\Desktop\gg1.cpp(39) : error C2040: '+' : 'class circle &(class Trapezoid &)' differs in levels of indirection from 'int (class Trapezoid &)'
C:\Users\me\Desktop\gg1.cpp(39) : error C2027: use of undefined type 'Trapezoid'
        C:\Users\me\Desktop\gg1.cpp(14) : see declaration of 'Trapezoid'
C:\Users\me\Desktop\gg1.cpp(39) : error C2228: left of '.getx' must have class/struct/union type
C:\Users\me\Desktop\gg1.cpp(39) : error C2027: use of undefined type 'Trapezoid'
        C:\Users\me\Desktop\gg1.cpp(14) : see declaration of 'Trapezoid'
C:\Users\me\Desktop\gg1.cpp(39) : error C2228: left of '.gety' must have class/struct/union type
C:\Users\me\Desktop\gg1.cpp(39) : error C2027: use of undefined type 'Trapezoid'
        C:\Users\me\Desktop\gg1.cpp(14) : see declaration of 'Trapezoid'
C:\Users\me\Desktop\gg1.cpp(39) : error C2228: left of '.max' must have class/struct/union type
执行 cl.exe 时出错.

gg1.exe - 1 error(s), 0 warning(s)
原来的错误依旧没有解决e~~好像还出现了重载的错误
#10
ml2325282010-12-12 12:09
已改好

#include"stdafx.h"
#include <iostream>
using namespace std;

class Shape
{public:
    virtual double area() const {return 0.0;}  
    virtual double max() const {return 0.0;}  
    virtual void shapeName() const =0;            
};
class Trapezoid;
class circle:public Shape
{
public:
    circle(double=0,double=0,double=0);
    void setcircle(double,double,double);
    void Judgement(circle &p);
    virtual double area();
    virtual void shapeName() const {cout<<"circle:";}
    circle operator + (Trapezoid &t);
    circle operator + (circle &c);
private:
    double x;
    double y;
    double ra;
    friend class Trapezoid;
};
class Trapezoid:public circle
{
    Trapezoid(double=0,double=0,double=0);
    double getx();
    double gety();
    virtual double max() const;
    void Judgement(Trapezoid &t);
private:
    double top;
    double bot;
    double hig;
    friend class circle;
};
void circle::setcircle(double a,double b,double r)
{ x=a;y=b;ra=r;}
circle::circle(double a,double b,double r)
{x=a;y=b;ra=r;}
void circle::Judgement(circle &c)
{if (ra>=0)
cout<<"是个圆"<<endl;
}
circle circle::operator + (Trapezoid &t)
{ return circle(x+t.getx(),y+t.gety(),ra+t.max());}

double circle::area()
{cout<<"area="<<ra*ra*3.1415<<endl;
return 0;}


double Trapezoid::getx()
{return top;}
double Trapezoid::gety()
{return bot;}
double Trapezoid::max () const   
{
    double m,n;
    m=top>bot?top:bot;   
    n=hig>m?hig:m;        
    return n;
}
void Trapezoid::Judgement(Trapezoid &t)
{
    if(top>0 && bot>0 && hig>0)
        cout<<"是一个梯形!"<<endl;
    else
        cout<<"不是一个梯形!"<<endl;
}

Trapezoid::Trapezoid(double a,double b,double h)
{top=a;bot=b;hig=h;}

int main(int argc, char* argv[])
{
    printf("Hello World!\n");
    getchar();
    return 0;
}
#11
侯三三2010-12-12 15:54
#include<iostream.h>
1