queue <string&>错误

Min*_*Ant 3 c++ string queue reference data-structures

我有这个有趣的情况.

我有一堆结构字符串.

struct foo
{
    string mStringName;
}
vector<foo> mFoos;
Run Code Online (Sandbox Code Playgroud)

我也有一个字符串引用队列

queue<string&> mStringQueue;
Run Code Online (Sandbox Code Playgroud)

最后,我有一个接受const字符串的函数&

void Bar(const string&);
Run Code Online (Sandbox Code Playgroud)

继承人的情况.

//...in some loop
currentFoo = mFoos[index];

// Queue up the string name.
mStringQueue.push(currentFoo.mStringName);


//...Later on, go through our queue and pass each one to the function.

for (int queueIndex = 0; queueIndex < mStringQueue.size(); queueIndex++)
{
    Bar(mStringQueue.front());
    mStringQueue.pop();
}
Run Code Online (Sandbox Code Playgroud)

这给了我以下编译错误:

错误C2664: '的std ::队列<_Ty> ::推':无法从 '字符串' 到 '串(&)' 转换参数1

我是definitley无法绕过字符串引用和诸如此类的东西,所以任何帮助都将非常感激

Dav*_*eas 6

引用类型不符合可在标准容器中使用的类型的要求.特别是它们不可复制.请注意,虽然引用的对象可以是可复制的,但引用本身永远不可复制.

另一种方法是存储可复制的指针.