标题:pat编译错误,error: passing 'const record' as 'this' argument of 'bool ...
只看楼主
c17767920
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-5-11
结帖率:100%
已结贴  问题点数:20 回复次数:2 
pat编译错误,error: passing 'const record' as 'this' argument of 'bool record::operat
In file included from /usr/include/c++/4.7/algorithm:63:0,
                 from a.cpp:9:
/usr/include/c++/4.7/bits/stl_algo.h: In instantiation of '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<record*, std::vector<record> >; _Tp = record]':
/usr/include/c++/4.7/bits/stl_algo.h:2309:70:   required from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<record*, std::vector<record> >]'
/usr/include/c++/4.7/bits/stl_algo.h:2340:54:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<record*, std::vector<record> >; _Size = long int]'
/usr/include/c++/4.7/bits/stl_algo.h:5476:4:   required from 'void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<record*, std::vector<record> >]'
a.cpp:66:45:   required from here
/usr/include/c++/4.7/bits/stl_algo.h:2271:4: error: passing 'const record' as 'this' argument of 'bool record::operator<(record)' discards qualifiers [-fpermissive]

这是pat的甲级1016题的一次提交,在vs2013上可以运行,上面是编译错误的信息。

代码:

//#include "stdafx.h"  这个在pat上去掉的
#include <time.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
struct record{
    time_t t;
    bool z;
    bool valid = 0;
    bool operator < (const record bsl){
        return t < bsl.t;
    };
    bool operator > (const record bsl){
        return t > bsl.t;
    };
};
int main(void)
{
    int cents[24], cday = 0;
    int N;
    int amount = 0, i, mon;
    string mgz, shj, zht;
    time_t rlt = time(NULL);
    tm * bdt,* bdt2;
    map<string, vector<record>> yonghu;
    map<string, int> jifei;
    record jilu;
    map<string, vector<record>>::iterator ddq;
    vector<record>::iterator diedai, diedai2;
    bdt = localtime(&rlt);
    for (i = 0; i < 24; i++){
        cin >> cents[i];
        cday += cents[i];
    }
    cin >> N;
    for (i = 0; i < N; i++){
        int date,hour, min;
        cin >> mgz;
        scanf("%d:%d:%d:%d", &mon, &date, &hour, &min);
        cin >> zht;
        bdt->tm_sec = 30;
        bdt->tm_mon = mon;
        bdt->tm_mday = date;
        bdt->tm_hour = hour;
        bdt->tm_min = min;
        if (zht == "on-line"){
            jilu.z = true;
            bdt->tm_sec = 20;
        }
        else{
            jilu.z = false;
            bdt->tm_sec = 0;
        }
        rlt = mktime(bdt);
        jilu.t = rlt;
        yonghu[mgz].push_back(jilu);
        jifei[mgz] = 0;
    }
    for (ddq = yonghu.begin(); ddq != yonghu.end(); ddq++){    // 各记录按时间顺序排序
        sort(ddq->second.begin(),ddq->second.end());
    }
    for (ddq = yonghu.begin(); ddq != yonghu.end(); ddq++){
        cout << ddq->first << " " << setfill('0') << setw(2) << mon << endl;
        for (diedai = ddq->second.begin(); diedai != ddq->second.end();diedai++){
            diedai2 = diedai + 1;
            if (diedai2 == ddq->second.end())
                break;
            if ((diedai->z == true) && (diedai2->z == false)){
                diedai->valid = true;
                diedai2->valid = true;
                int dy[2], hr[2], mn[2], charge[2];
                bdt = localtime(&(diedai->t));
                dy[0] = bdt->tm_mday; hr[0] = bdt->tm_hour; mn[0] = bdt->tm_min;
                bdt2 = localtime(&(diedai2->t));
                dy[1] = bdt2->tm_mday; hr[1] = bdt2->tm_hour; mn[1] = bdt2->tm_min;
                printf("%02d:%02d:%02d ", dy[0], hr[0], mn[0]);
                printf("%02d:%02d:%02d ", dy[1], hr[1], mn[1]);
                for (int k = 0; k < 2; k++){
                    charge[k] = cday*dy[k] * 60;
                    for (int temp = 0; temp < hr[k]; temp++)
                        charge[k] += cents[temp] * 60;
                    charge[k] += cents[hr[k]] * mn[k];
                }
                amount = charge[1] - charge[0];
                i = 1 + difftime(diedai2->t, diedai->t) / 60;
                cout << i << " $" <<fixed<< setprecision(2) << amount / 100.0 << endl;
                jifei[ddq->first] += amount;
                diedai++;
            }
        }
        cout << "Total amount: $" <<fixed<< setprecision(2) << jifei[ddq->first] / 100.0 << endl;
    }
    return 0;
}


求助,看不懂错误原因
搜索更多相关主题的帖子: required passing include record 
2016-05-11 17:10
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
没有你这个编译器,但出错的提示信息还是蛮清楚的
passing 'const record' as 'this' argument of 'bool record::operator<(record)'
既然是 const record,那么自然只能调用const member function

bool operator < (const record bsl)
改为
bool operator<( const record& bsl ) const
试试,其它雷同
2016-05-12 09:54
c17767920
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-5-11
得分:0 
回复 2楼 rjsp
解决了,虽然还不太懂,谢谢你
2016-05-13 14:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-464660-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012784 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved