注册 登录
编程论坛 C++教室

一道关于类的问题

枫xby 发布于 2015-05-02 20:14, 648 次点击
代码如下  求各位指导

#include<iostream>
using namespace std;
int main(){
    double f,g;
    double l,m,n;
   cout<<"请输入两个双精度浮点数:";
   cin>>f>>g;
   cout<<"请输入三个精度浮点数:";
   cin>>l>>m>>n;
   Compare compare1(f,g);
   Compare compare2(l,m,n);
   compare1.min();
   compare2.min_1();
   compare1.show();
   compare2.show_1();

   return 0;
   

}
class Compare
{  private:
     double t1;
     double t2;
     double x,y,z;
     double a,b;
  
   public:
      Compare(double ,double,double);
      Compare(double,double);
   
      void min();
      void min_1();
      void show();
      void show_1();

};
void Compare::min()
{   if(a>b)
       t1=b;
    else
       t1=b;
}
void Compare::min_1()
{
     double temp;
     if(x>y)
         temp=y;
     else
         temp=x;
     if(z>temp)
         t2=temp;
     else
         t2=z;

}
void Compare::show(){
    cout<<t1;
}
void Compare::show_1(){
    cout<<t2;
}
Compare::Compare(double f,double g)
{
    a=f;
    b=g;
}
Compare::Compare(double l,double m,double n)
{
    x=l;
    y=m;
    z=n;
}

错误显示为
D:\C++\test1\test1.cpp(10) : error C2065: 'Compare' : undeclared identifier
D:\C++\test1\test1.cpp(10) : error C2146: syntax error : missing ';' before identifier 'compare1'
D:\C++\test1\test1.cpp(10) : error C2065: 'compare1' : undeclared identifier
D:\C++\test1\test1.cpp(11) : error C2146: syntax error : missing ';' before identifier 'compare2'
D:\C++\test1\test1.cpp(11) : error C2065: 'compare2' : undeclared identifier
D:\C++\test1\test1.cpp(12) : error C2228: left of '.min' must have class/struct/union type
D:\C++\test1\test1.cpp(13) : error C2228: left of '.min_1' must have class/struct/union type
D:\C++\test1\test1.cpp(14) : error C2228: left of '.show' must have class/struct/union type
D:\C++\test1\test1.cpp(15) : error C2228: left of '.show_1' must have class/struct/union type
3 回复
#2
yangfrancis2015-05-02 21:38
把类的声明写在main函数前面去
#3
林月儿2015-05-02 21:42
位置调一下,main函数下移到最后
#4
yangfrancis2015-05-02 21:44
这儿还有个逻辑错误:
void Compare::min()
{   if(a>b)
       t1=b;
    else
       t1=b;
}
对t1赋值没有改动
1