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

请指教一下下面这个程序的问题,编译的时候是if语句有问题,难道来里面成员函数的的结果不能作比较??

yljyljylj 发布于 2013-09-25 09:10, 522 次点击
#include"iostream"
using namespace std;
class student
{public:
 student(int,int,int);
 int complex();
    int age;
    int height;
    int weight;
    };
student::student(int h,int w,int a)
{height=h;
 weight=w;
 age=a;
}
 int student::complex()
{int z;
 z=(height-weight)/2+age;
 return z;

}
main()
{student stu1(172,60,22),stu2(180,70,25);
cout<<"the compex is "<< ()<<endl;
cout<<"age is  "<<stu1.age<<endl<<"height is  "<<stu1.height<<endl<<"weight is "<<stu1.weight<<endl;
cout<<"the compex is "<< ()<<endl;
cout<<"age is  "<<stu2.age<<endl<<"height is  "<<stu2.height<<endl<<"weight is "<<stu2.weight<<endl;
if( ()>int ())
cout<<"stu1 is more healthy";
else
cout<<"stu2 is more healthy";
return 0;
}
提示的错误是:D:\VC store\practice 03.cpp(28) : error C2440: 'type cast' : cannot convert from 'int (__thiscall student::*)(void)' to 'int'
请指教
7 回复
#2
3037709572013-09-25 09:24
分析如下:
#include  <iostream>
using namespace std;
class student
{
public:
    student(int,int,int);
    int complex();
    int age;
    int height;
    int weight;
};
student::student(int h,int w,int a)
{
    height=h;
    weight=w;
    age=a;
}
int student::complex()
{
    int z;
    z=(height-weight)/2+age;
    return z;

}
int main()//既然用 return 0语句了必须有返回值。
{
    student stu1(172,60,22),stu2(180,70,25);
    cout<<"the compex is "<< ()<<endl;
    cout<<"age is  "<<stu1.age<<endl<<"height is  "<<stu1.height<<endl<<"weight is "<<stu1.weight<<endl;
    cout<<"the compex is "<< ()<<endl;
    cout<<"age is  "<<stu2.age<<endl<<"height is  "<<stu2.height<<endl<<"weight is "<<stu2.weight<<endl;
    if( (())>int (()))//这是函数调用结果的比较不是属性或者变量值得比较,要加括号的。
        cout<<"stu1 is more healthy";
    else
        cout<<"stu2 is more healthy";
    return 0;
}
#3
mcm_mingge2013-09-25 10:52
坑爹啊,函数调用要加 ()滴!
#4
mcm_mingge2013-09-25 10:58
坑爹啊,函数调用要加 ()滴!
#5
yljyljylj2013-09-25 11:46
回复 2楼 303770957
谢谢了,是自己的问题,调用函数结果的时候确实要加括号,因为main函数默认的返回值类型是int,所以就没加,再请假一个问题,在VC里面怎么装MSDN?
#6
3037709572013-09-25 11:49
问题太多,先结贴在回答下一个问题。
#7
林兴杰2013-09-25 12:23
#include<iostream>
using namespace std;
class student
{
public:
    student(int,int,int);
    int age;
    int height;
    int weight;
    int complex();
};
student::student(int h,int w,int a)
{
    height=h;
    weight=w;
    age=a;
}
int student::complex()
{
    int z;
    z=(height-weight)/2+age;
    return z;

}
main()
{
    student stu1(172,60,22),stu2(180,70,25);
    cout<<"the compex is "<< ()<<endl;
    cout<<"age is  "<<stu1.age<<endl<<"height is  "<<stu1.height<<endl<<"weight is "<<stu1.weight<<endl;
    cout<<"the compex is "<< ()<<endl;
    cout<<"age is  "<<stu2.age<<endl<<"height is  "<<stu2.height<<endl<<"weight is "<<stu2.weight<<endl;
    if( (())>(()))
    cout<<"stu1 is more healthy";
    else
    cout<<"stu2 is more healthy";
    system("PAUSE");
    return 0;
}
同学 首先你的书写要规范点  其次我改的这些要注意 ,system("PAUSE")这个不能省略了
#8
3037709572013-09-25 22:49
可以直接下載VC的msdn進行安裝。好像是2002版的,就是孫鑫的c++視頻中用的那個。
1