1

从现在开始,我一直在寻找这个问题的根源。帮助将不胜感激。

#ifndef SETFRAMENODE_H_
#define SETFRAMENODE_H_

#include <time.h>

class SetFrameNode {
private:
    double rate;
    time_t timeStamp;
public:
    SetFrameNode():rate(0),timeStamp(0){}
    SetFrameNode(double rate, time_t timeStamp): rate(rate), timeStamp(timeStamp){
    }
    virtual ~SetFrameNode(){};
    virtual bool operator<(const HeapFrameNode& rhs) const
    {
        if (this->rate>=0){
            if (rhs.rate>=0){
                if(this->rate == rhs.rate){
                    return (this->timeStamp < rhs.timeStamp) ;
                }
                return (this->rate < rhs.rate) ;
            }
        return false;
        }
        return true;
    }
    time_t getTime(){
        return timeStamp;
    }
    double getRate() const{
        return rate;
    }
};

class SetFrameTime: public SetFrameNode{
public:
    SetFrameTime(): SetFrameNode(){}
    SetFrameTime(double rate, time_t timeStamp): SetFrameNode(rate,timeStamp){
    }
    virtual bool operator<(SetFrameNode rhs){
        return (this->getTime() < rhs.getTime());
    }

};

#endif 

我遇到问题的功能是:

class FrameList {
    list<FrameNode> pointsList;
    time_t timeStamp;
    set<SetFrameNode>* frameSetR;
    set<SetFrameTime>* frameSetT;
    double rate;
public:
    FrameList(set<SetFrameNode>* frameSetR, set<SetFrameTime>* frameSetT):frameSetR(frameSetR),frameSetT(frameSetT),rate(0){
        timeStamp=time(NULL);
    }
    virtual ~FrameList(){
           list<FrameNode>::iterator i;
           PointFrame* ptr;
           double oldRate=0;
           for(i=pointsList.begin(); i != pointsList.end(); ++i){
               ptr=(i->getTrajectory()).deleteFrame(i->getPoint());
               if(ptr==NULL){
                   continue;
               }
               SetFrameTime newFrameTime(0,ptr->getTimeStamp());
               if(frameSetT->find(newFrameTime) ==frameSetT->end()){
                   throw 98;
               }
               oldRate=(frameSetT->find(newFrameTime))->getRate();
               SetFrameNode newFrameNode1(oldRate,ptr->getTimeStamp());
               frameSetR->erase(newFrameNode1);
               oldRate =+ ptr->getRateDiff();
               SetFrameNode newFrameNode2(oldRate,ptr->getTimeStamp());
               frameSetR->insert(newFrameNode2);
               SetFrameTime newFrameTime2(oldRate,ptr->getTimeStamp());
               frameSetT->erase(newFrameTime);
               frameSetT->insert(newFrameTime);

           }
    }

我得到一个“从这里实例化”的错误if(frameSetT->find(newFrameTime) ==frameSetT->end())frameSetR->insert(newFrameNode2);任何想法?

这是完整的信息:

make all 
Building file: ../Frame.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Frame.d" -MT"Frame.d" -o "Frame.o" "../Frame.cpp"
In file included from /usr/include/c++/4.6/bits/stl_tree.h:65:0,
                 from /usr/include/c++/4.6/set:60,
                 from ../Frame.h:12,
                 from ../Frame.cpp:8:
/usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = SetFrameTime]’:
/usr/include/c++/4.6/bits/stl_tree.h:1529:38:   instantiated from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) [with _Key = SetFrameTime, _Val = SetFrameTime, _KeyOfValue = std::_Identity<SetFrameTime>, _Compare = std::less<SetFrameTime>, _Alloc = std::allocator<SetFrameTime>, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<SetFrameTime>]’
/usr/include/c++/4.6/bits/stl_set.h:605:29:   instantiated from ‘std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::find(const key_type&) [with _Key = SetFrameTime, _Compare = std::less<SetFrameTime>, _Alloc = std::allocator<SetFrameTime>, std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<SetFrameTime>, std::set<_Key, _Compare, _Alloc>::key_type = SetFrameTime]’
../Frame.h:61:38:   instantiated from here
/usr/include/c++/4.6/bits/stl_function.h:236:22: error: passing ‘const SetFrameTime’ as ‘this’ argument of ‘virtual bool SetFrameTime::operator<(SetFrameNode)’ discards qualifiers [-fpermissive]
/usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = SetFrameNode]’:
/usr/include/c++/4.6/bits/stl_tree.h:1267:4:   instantiated from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = SetFrameNode, _Val = SetFrameNode, _KeyOfValue = std::_Identity<SetFrameNode>, _Compare = std::less<SetFrameNode>, _Alloc = std::allocator<SetFrameNode>]’
/usr/include/c++/4.6/bits/stl_set.h:410:29:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = SetFrameNode, _Compare = std::less<SetFrameNode>, _Alloc = std::allocator<SetFrameNode>, typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<SetFrameNode>, std::set<_Key, _Compare, _Alloc>::value_type = SetFrameNode]’
../Frame.h:69:38:   instantiated from here
/usr/include/c++/4.6/bits/stl_function.h:236:22: error: passing ‘const SetFrameNode’ as ‘this’ argument of ‘virtual bool SetFrameNode::operator<(SetFrameNode)’ discards qualifiers [-fpermissive]
make: *** [Frame.o] Error 1
4

2 回答 2

3

重要的错误信息是

/usr/include/c++/4.6/bits/stl_function.h:236:22: error: passing ‘const SetFrameTime’ as ‘this’ argument of ‘virtual bool SetFrameTime::operator<(SetFrameNode)’ discards qualifiers [-fpermissive]

前面几行只是试图告诉您编译器实例化了哪些模板来查找错误。

问题的根本原因是std::set试图调用operator<-qualified constSetFrameTime但本身SetFrameTime::operator<()不是const-qualified 。只需const在不更改其操作对象的任何运算符(或成员函数)之后添加即可。

于 2013-01-02T18:10:51.947 回答
3

随着添加的错误消息,我相信这一点

bool operator<(HeapFrameNode rhs)

应该

bool operator<(const HeapFrameNode& rhs) const

以某种向后的方式,编译器试图告诉您您在常量对象上调用了非常量运算符。

于 2013-01-02T18:08:09.990 回答