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

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

我有一个结构 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) { }

    ...
};

还有一个 class distributor 通过 SystemC sc_in 接收 msgs 并将一些元素推送到二维向量 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);
            }
        }

        ...
};

注释行出现以下错误:

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

为什么会出现错误?

buffer.at(newMessage.addr)std::vector <msg> 所以它应该采用 msg 类型的对象,即 newMessage...

感谢您的帮助!

std::vector::insert 迭代器 作为第一个参数,而不是 索引。 你可能想要这个:

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);

我已将调用包装在一个函数中,因为它引用了向量两次,无论如何它都会使代码更好读。