簡體   English   中英

無法將 `boost::assign::list_of` 分配給 `static const std::vector`

[英]not able to assign `boost::assign::list_of` to `static const std::vector`

我正在使用 C++ 03,以前我有代碼:

static const std::vector<std::pair<uint16_t, uint16_t>> a = boost::assign::list_of(std::make_pair(1,1)) std::make_pair(2,2));

它奏效了。

現在我需要添加一個 If 塊,在不同的情況下a需要是不同的值,所以我有:

static const std::vector<std::pair<uint16_t, uint16_t>> a;
if (xxx){
    a = boost::assign::list_of(std::make_pair(1,1)) std::make_pair(2,2));
}
else{
    a = boost::assign::list_of(std::make_pair(1,1)) std::make_pair(3,3));
}

我得到了error:ambiguious overload for operator= ..........我該如何解決?

如果您仔細查看錯誤消息,您可能會發現提到無法轉換為const 作為a is const你不能分配給它。 您需要創建aconst或者您需要在分配時對其進行初始化。 您可以使用三元組來執行此操作:

static const std::vector<std::pair<uint16_t, uint16_t> > a = xxx ?
    boost::assign::list_of(std::make_pair(1,1))(std::make_pair(2,2)) :
    boost::assign::list_of(std::make_pair(1,1))(std::make_pair(3,3));

或者通過使用一個函數:

std::vector<std::pair<uint16_t, uint16_t> > make_a()
{
    if (xxx)
    {
        return boost::assign::list_of(std::make_pair(1,1))(std::make_pair(2,2));
    }
    else
    {
        return boost::assign::list_of(std::make_pair(1,1))(std::make_pair(3,3));
    }
}
static const std::vector<std::pair<uint16_t, uint16_t> > a = make_a();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM