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

这是一个把学生按总分成绩排序的程序,但他vs2012这里说rank不明确是什么意思

白金之星 发布于 2018-06-09 19:19, 3935 次点击
#include<iostream>
#include<string>
using namespace std;

template<class T>
void rank(T *a)
{
    T temp;
    int i,j;
    for(i=0;i<5;i++)
    {
        for(j=0;j<(6-i-1);j++)
        {
            if((a+j)->sum>(a+j+1)->sum)
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
   
};

class student
{
public:
    char name[20];
    long int ID;
    int mathscore;
    int chinesescore;
    int englishscore;
    int sum;
public:
    student(char *n=0,long int id=0,int ms=0,int cs=0,int es=0)
    {
        *name=*n;
        ID=id;
        mathscore=ms;
        chinesescore=cs;
        englishscore=es;
    }
    void add()
    {
        sum=mathscore+chinesescore+englishscore;
    }
   
};

void main()
{   
    int i,a[6];
    student stu[6]={student("zhang",001,80,86,90),student("han",002,98,85,85),student("he",003,86,94,82),
                    student("liu",004,98,95,96),student("fang",005,98,78,86),student("gao",006,95,85,74)};
   
    for(i=0;i<6;i++)
    {
        stu[i].add();
        cout<<stu[i].name<<"\t"<<stu[i].ID<<"\t"<<stu[i].mathscore<<"\t"<<
            stu[i].chinesescore<<"\t"<<stu[i].englishscore<<"\t"<<endl;
    }
   
    for(i=0;i<6;i++)
    {
        a[i]=stu[i].sum;
    }

    rank(stu);
   
    for(i=0;i<6;i++)
    {
        cout<<stu[i].name<<"\t"<<stu[i].ID<<"\t"<<stu[i].mathscore<<"\t"<<
            stu[i].chinesescore<<"\t"<<stu[i].englishscore<<"\t"<<endl;
    }
   
   
}
只有本站会员才能查看附件,请 登录
16 回复
#2
Jonny02012018-06-10 11:52
一看就是不会用模板还要硬来的
把模板函数改成普通函数试一下
#3
白金之星2018-06-10 15:11
回复 2楼 Jonny0201
这是我同学的  她头巨铁 我叫她用普通函数 她偏要说“我就想知道自己怎么错了”
然后嘲讽我找不出问题
#4
白金之星2018-06-10 16:03
回复 2楼 Jonny0201
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
    char name[20];
    long int ID;
    int mathscore;
    int chinesescore;
    int englishscore;
    int sum;
public:
    student(char n[20]=0,long int id=0,int ms=0,int cs=0,int es=0)
    {
        strcpy(name,n);
        ID=id;
        mathscore=ms;
        chinesescore=cs;
        englishscore=es;
    }
    void add()
    {
        sum=mathscore+chinesescore+englishscore;
    }
   
};
void rank(student *a)
{
    student temp;
    int i,j;
    for(i=0;i<5;i++)
    {
        for(j=0;j<(6-i-1);j++)
        {
            if(a[j].sum>a[j+1].sum)
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
   
};

void main()
{   
    int i;
    student stu[6]={student("zhang",001,80,86,90),student("han",002,98,85,85),student("he",003,86,94,82),
                    student("liu",004,98,95,96),student("fang",005,98,78,86),student("gao",006,95,85,74)};
   
    for(i=0;i<6;i++)
    {
        stu[i].add();
        cout<<stu[i].name<<"\t"<<stu[i].ID<<"\t"<<stu[i].mathscore<<"\t"<<
            stu[i].chinesescore<<"\t"<<stu[i].englishscore<<"\t"<<endl;
    }
   
    rank(stu);
   
    for(i=0;i<6;i++)
    {
        cout<<stu[i].name<<"\t"<<stu[i].ID<<"\t"<<stu[i].mathscore<<"\t"<<
            stu[i].chinesescore<<"\t"<<stu[i].englishscore<<"\t"<<endl;
    }
   
   
}
#5
白金之星2018-06-10 16:05
回复 4楼 白金之星
这是我用vc6.0改了的但是在rank()函数那终止运行了 没语法错误 应该是逻辑出问题了 我实在是找不出来了
#6
白金之星2018-06-10 16:05
回复 4楼 白金之星
这是我用vc6.0改了的但是在rank()函数那终止运行了 没语法错误 应该是逻辑出问题了 我实在是找不出来了
#7
Jonny02012018-06-10 23:49
回复 6楼 白金之星
改成指针是因为类里面有数组或者指针
否则会造成使用野指针
void rank(student *a)
{
    student *temp;
    int i,j;
    for(i=0;i<5;i++)
    {
        for(j=0;j<(6-i-1);j++)
        {
            if(a[j].sum>a[j+1].sum)
            {
                temp=&a[j];
                a[j]=a[j+1];
                a[j+1]=*temp;
            }
        }
    }

};

[此贴子已经被作者于2018-6-10 23:56编辑过]

#8
Jonny02012018-06-10 23:49
不过你的代码应该是有问题的, 最后的输出结果
zhang    1    80    86    90   
han    2    98    85    85   
he    3    86    94    82   
liu    4    98    95    96   
fang    5    98    78    86   
gao    6    95    85    74   
gao    6    95    85    74   
gao    6    95    85    74   
gao    6    95    85    74   
gao    6    95    85    74   
gao    6    95    85    74   
gao    6    95    85    74   
#9
Jonny02012018-06-10 23:55
还有, C++ 标准库里面有一个叫 rank 的类
你这个和标准库里面的冲突了
你这个标准的术语是 模棱两可的呼叫 ambiguous calling
解决方案大致有两种
using namespace std;
改成
using std::cout;
using std::endl;
另外就是把 rank 写进类别里面
class student {
    static void rank(student *);
}
#10
Jonny02012018-06-11 00:01
我帮你改了一下
程序代码:
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
    string name;
    long int ID;
    int mathscore;
    int chinesescore;
    int englishscore;
    int sum;
public:
    student(const string &name = string(),long int id=0,int ms=0,int cs=0,int es=0)
    {
        this->name = name;
        ID=id;
        mathscore=ms;
        chinesescore=cs;
        englishscore=es;
    }
    void add()
    {
        sum=mathscore+chinesescore+englishscore;
    }

};
int main()
{
    int i;
    student stu[6]={student("zhang",001,80,86,90),student("han",002,98,85,85),student("he",003,86,94,82),
                    student("liu",004,98,95,96),student("fang",005,98,78,86),student("gao",006,95,85,74)};

    for(i=0;i<6;i++)
    {
        stu[i].add();
        cout<<stu[i].name<<"\t"<<stu[i].ID<<"\t"<<stu[i].mathscore<<"\t"<<
            stu[i].chinesescore<<"\t"<<stu[i].englishscore<<"\t"<<endl;
    }

    sort(stu, stu + 6, [](const student &a, const student &b) -> bool {
        return a.sum > b.sum;
    });

    for(i=0;i<6;i++)
    {
        cout<<stu[i].name<<"\t"<<stu[i].ID<<"\t"<<stu[i].mathscore<<"\t"<<
            stu[i].chinesescore<<"\t"<<stu[i].englishscore<<"\t"<<endl;
    }


}
#11
白金之星2018-06-11 09:14
回复 10楼 Jonny0201
大神   谢谢!!!收下我的膝盖!!
#12
白金之星2018-06-11 09:32
回复 10楼 Jonny0201
额  大佬你这sort是什么  没定义啊 我有点看不懂
#13
Jonny02012018-06-11 14:09
回复 12楼 白金之星
sort 是标准库函数, 相当于别人给你写好的
你平时用的 cout 和 cin 都是别人给你写好的, 包括 printf 和 scanf
你可以自行搜索 C++ 的 sort 和 lambda
你能看懂网上的教学就能看懂这段代码
#14
白金之星2018-06-12 16:45
大佬  有错误

C:\Users\13414\Desktop\C语言\test.cpp(42) : error C2059: syntax error : '['
C:\Users\13414\Desktop\C语言\test.cpp(42) : error C2143: syntax error : missing ')' before '{'
C:\Users\13414\Desktop\C语言\test.cpp(42) : error C2143: syntax error : missing ';' before '{'
C:\Users\13414\Desktop\C语言\test.cpp(42) : error C2065: 'a' : undeclared identifier
C:\Users\13414\Desktop\C语言\test.cpp(42) : error C2228: left of '.sum' must have class/struct/union type
C:\Users\13414\Desktop\C语言\test.cpp(42) : error C2065: 'b' : undeclared identifier
C:\Users\13414\Desktop\C语言\test.cpp(42) : error C2228: left of '.sum' must have class/struct/union type
C:\Users\13414\Desktop\C语言\test.cpp(42) : warning C4508: 'main' : function should return a value; 'void' return type assumed
C:\Users\13414\Desktop\C语言\test.cpp(42) : error C2059: syntax error : ')'
执行 cl.exe 时出错.


#15
白金之星2018-06-12 16:46
回复 14楼 白金之星
额  vs没问题  vc有问题
#16
白金之星2018-06-12 16:48
回复 15楼 白金之星
好的谢谢大佬 没问题了
1