简体   繁体   English

C ++ 11没有匹配的函数来调用'std :: vector

[英]C++11 no matching function for call to ‘std::vector

I have a struct msg : 我有一个结构msg

struct msg {
    //destination port
    int addr;
    //data
    unsigned long long payload;

    //prioritized flag
    bool isPrio;

    //construcor?
    msg(int a, int p, bool b) : addr(a), payload(p),isPrio(b) { }

    msg() : addr(0), payload(0), isPrio(false) { }

    ...
};

And a class distributor which receives msg s via SystemC sc_in and pushes some elements to a 2 dimensional vector std::vector<std::vector <msg>> buffer : 一个类distributor ,它通过SystemC sc_in接收msg ,并将某些元素推送到二维向量std::vector<std::vector <msg>> buffer

class distributor: public sc_module {
    public:
        sc_vector<sc_in<msg>> inputMsg;
        std::vector<std::vector <msg>> buffer;
        int n, m, bufferSize;
        ...


        distributor(sc_module_name name, int n, int m, int bufferSize) : //n -> number of inputs, m -> number of outputs
            sc_module(name),
            inputMsg("inputMsg", n),
            n(n),
            m(m),
            buffer(m),
            bufferSize(bufferSize)
            ...
        {
            SC_HAS_PROCESS(distributor);
            SC_METHOD(receive); sensitive << ...;
            ...
        }

        void receive() {
            for(int i = 0; i < n; i++){
                msg newMessage = inputMsg.at(i).read();
                if(buffer.at(newMessage.addr).size() >= bufferSize) continue;
                if(newMessage.isPrio) buffer.at(newMessage.addr).insert(0, newMessage); //<- ERROR OCCURS HERE
                else buffer.at(newMessage.addr).push_back(newMessage);
            }
        }

        ...
};

In the commented line the following error occurs: 在注释行中,发生以下错误:

error: no matching function for call to ‘std::vector<msg>::insert(int, msg&)’
     if(newMessage.isPrio) buffer.at(newMessage.addr).insert(0, newMessage);
                                                                          ^

Why does the error occur ? 为什么会发生错误?

buffer.at(newMessage.addr) is std::vector <msg> so it should take an object of type msg which is newMessage ... buffer.at(newMessage.addr)std::vector <msg>因此它应采用msg类型的对象,即newMessage ...

I appreciate your help! 我感谢您的帮助!

std::vector::insert takes an iterator as the first argument, not an index. std::vector::insert迭代器作为第一个参数,而不是索引。 You probably want this: 您可能想要这样:

void add_to_front(std::vector<msg> &vector, const msg &message)
{
  vector.insert(begin(vector), message);
}


if(newMessage.isPrio) add_to_front(buffer.at(newMessage.addr), newMessage);

I've wrapped the call in a function because it references the vector twice, and it will make the code read better anyway. 我将调用包装在一个函数中,因为它两次引用了向量,并且无论如何都会使代码更好地读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM