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

一个c++错误请大家指点

wghost 发布于 2009-10-27 12:53, 1683 次点击
#include<iostream>
using namespace std;
#include"student.h"
int main()
{   void max(student *p1);
    student stud[3]={student(1,56.8),student(2,89.3),student(3,78.3)},*p=stud;
    max(stud);
    return 0;
}
void max(student *p1)
{int i,j;
double t;
 
 for(i=0;i<2;i++)
    for(j=i+1;j<3;j++)
    if(*(p1+i)<*(p1+j))
     {t=p1[i];p1[i]=p1[j];p1[j]=t;}
}
其中红色部分提示一下错误:
D:\c++\ex3_h51\f.cpp(16) : error C2784: 'bool __cdecl std::operator <(const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template argument for 'const class std::revers
e_iterator<_RI,_Ty,_Rt,_Pt,_D> &' from 'class student'
D:\c++\ex3_h51\f.cpp(16) : error C2784: 'bool __cdecl std::operator <(const struct std::pair<_T1,_T2> &,const struct std::pair<_T1,_T2> &)' : could not deduce template argument for 'const struct std::pair<_T1,_T2> &' from 'class student'
D:\c++\ex3_h51\f.cpp(16) : error C2676: binary '<' : 'class student' does not define this operator or a conversion to a type acceptable to the predefined operator
D:\c++\ex3_h51\f.cpp(17) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class student' (or there is no acceptable conversion)
D:\c++\ex3_h51\f.cpp(17) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'double' (or there is no acceptable conversion)
Error executing cl.exe.
我是初学者,请指教!
4 回复
#2
qlc002009-10-27 13:11
void max(student *p1)
你这里面的p1只是定义了一个类指针,它跟对象数组不一样,一直接使用它来进行数值的比较是不对的。void max(student *p1)如果是p1指向对象数组然后在使用指针的运算,那是正确的。但是你现在没有让指针指向任何东西,却让它来做运算这肯定是错的!
#3
AngzAngy2009-10-27 13:44
改正:void max(student *p1)
{int i,j;
 student *t;                 //////double t; 在此处改正
 
for(i=0;i<2;i++)
    for(j=i+1;j<3;j++)
    if(*(p1+i)<*(p1+j))
     {t=p1[i];p1[i]=p1[j];p1[j]=t;}
}

#4
newCpp2009-10-27 22:48
程序代码:
#include<iostream>
using namespace std;
struct student
{
    int i;
    int j;
    int h;
};
int main()
{   
    void max(student *p1);
    student stud[3]={1,56,8,2,89,6,3,78,3};
    max(stud);
    return 0;
}
void max(student *p1)
{
int i,j;
int t;
 
for(i=0;i<3;i++)
{
cout<<p1->i<<" "<<p1->j<<" "<<p1->h<<endl;
p1++;
}
}
这玩意是是没有排序的,我也不清楚你要实现的啥功能,随便改了一下,自己参考参考吧!
#5
东海一鱼2009-10-27 23:01
编译器提示你的类型操作不匹配,也就是说在student这个类(结构)里,你没有重载'='、'>'、'<'这些操作符所以编译器无法处理这些代码。普通的操作符是对缺省变量类型操作的,无法对用户特定类型操作。除非你自己重载了这些操作符。
1