| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1244 人关注过本帖
标题:求解答重载运算符编译为什么不通过
只看楼主 加入收藏
printf0
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2010-7-13
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:10 
求解答重载运算符编译为什么不通过
// 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)
搜索更多相关主题的帖子: 算符 载运 编译 解答 
2010-12-09 14:20
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:0 
operator + (Trapezoid &t);
    operator + (circle &c);
没有返回值   至少要加个看你的代码应该是返回本类对象的引用

                                         
===========深入<----------------->浅出============
2010-12-09 15:20
printf0
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2010-7-13
收藏
得分:0 
circle::operator + (Trapezoid &t)
{ return circle(x+t.getx(),y+t.gety(),ra+t.max());}

有返回值啊
2010-12-09 16:36
printf0
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2010-7-13
收藏
得分:0 
提示是用力没有定义的Trapezoid。。。啊不解
2010-12-09 17:13
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:5 
circle::这是类标识

                                         
===========深入<----------------->浅出============
2010-12-09 21:55
printf0
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2010-7-13
收藏
得分:0 
回复 5楼 laoyang103
额 还是没有理解~~能不能把错的地方纠正一下写出来 谢谢了
2010-12-09 23:12
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:0 
你的那个运算符+ return后面是调用了一个构造函数  
返回了一个对象   所以你的返回值应该是本类的对象
circle &circle::operator + (Trapezoid &t)
{ return circle(x+t.getx(),y+t.gety(),ra+t.max());}
返回引用


                                         
===========深入<----------------->浅出============
2010-12-10 10:25
printf0
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2010-7-13
收藏
得分:0 
额 重载运算符需要这样定义?~~~
2010-12-10 12:44
printf0
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2010-7-13
收藏
得分:0 
回复 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~~好像还出现了重载的错误
2010-12-10 12:51
ml232528
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:367
专家分:879
注 册:2007-7-23
收藏
得分:15 
已改好

#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;
}

-︻┻┳═一 ☆ 悲伤的代价就是让自己明白什么是最重要的和应该珍惜的
2010-12-12 12:09
快速回复:求解答重载运算符编译为什么不通过
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037945 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved