从这里实例化错误

时间:2013-01-02 17:53:32

标签: c++ eclipse-cdt

我几天来一直在寻找这个问题的根源。将非常感谢帮助。

#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

2 个答案:

答案 0 :(得分:3)

添加错误消息后,我相信

bool operator<(HeapFrameNode rhs)

应该是

bool operator<(const HeapFrameNode& rhs) const

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

答案 1 :(得分: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< - 合格const上致电SetFrameTime,但SetFrameTime::operator<()不是{{} 1}} - 合格自己。只需在任何不想更改其操作对象的运算符(或成员函数)之后添加const