LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 610|回复: 3

构造函数和析构函数问题

[复制链接]
发表于 2007-1-29 14:18:34 | 显示全部楼层 |阅读模式
为什么要把/////////////////前面的注释掉才能编译通过

///overloading of function

#include<iostream>
using namespace std ;

class compare
{
     private:
        int a;
        int b;
        int c;
     public:
        //~compare();             /////////////////为什么要注释掉
        //compare();               /////////////////为什么要注释掉
        int comparing(int);
        char comparing(char);
        double comparing(double);
};

int compare:: comparing(int max)
{
     cout<<"lease input three integers!!"<<endl;
     int a,b,c;
     cin>>a >>b >>c;
     cout<<endl;
     if(b>a)
        a=b;
     if(c>a)
        a=c;
     max=a;
     cout<<"The max integer is "<< max<<endl;
     return max;
}

char compare:: comparing(char max)
{
cout<<"lease input three strings!!"<<endl;
     char a,b,c;
     cin>>a >>b >>c;
     cout<<endl;
     if(b>a)
        a=b;
     if(c>a)
        a=c;
     max=a;
     cout<<"The max string is "<< max<<endl;
     return max;
}

double compare:: comparing(double max)
{
     cout<<"lease input three floats!!"<<endl;
     double a,b,c;
     cin>>a >>b >>c;
     cout<<endl;
     if(b>a)
        a=b;
     if(c>a)
        a=c;
     max=a;
     cout<<"The max float is "<< max<<endl;
     return max;
}

int main()
{
     compare MAX;
     int flag;
     cout<<"lease input a value for flag!! The value must be 0 or 1 or 2"<<endl;
     cin>>flag;
     switch(flag)
             {
         case 0:
            int max1;
            MAX.comparing(max1);
            break;
         case 1:
            char max2;
            MAX.comparing(max2);
            break;
         case 2:
            double max3;
            MAX.comparing(max3);
            break;
         default:
            cout<<"input error!!"<<endl;
            exit(0);
               }
     
}
发表于 2007-1-29 15:40:48 | 显示全部楼层
大概是因为你没有定义构造和析构函数,只有声明
回复 支持 反对

使用道具 举报

发表于 2007-1-29 21:29:09 | 显示全部楼层
//~compare(); /////////////////为什么要注释掉
//compare(); /////////////////为什么要注释掉

这俩行改成如下
compare() {};
~compare() {};


编译完成,运行结果如下
[kevin@kevin C]$ g++ -o test a.cpp
[kevin@kevin C]$ ./test
Please input a value for flag!! The value must be 0 or 1 or 2
0
Please input three integers!!
2 3 4

The max integer is 4
回复 支持 反对

使用道具 举报

发表于 2007-1-29 22:25:36 | 显示全部楼层
多看看编译器给出来的信息啊!!!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表