在c++中给元素赋地址时出现错误

Getting error while assiging the address of an element in C++

本文关键字:错误 地址 c++ 元素      更新时间:2023-10-16

我有两个类,Class Aclass B。我的代码写在下面。我得到了一个我在最后提到的错误。

class A
{    
    Private:
        B Labels
        ....
        ....    
};
A::method()
{
    Labels.add (label_mark);
    ....
    ....
}
A::save()
{
....
....
    for (int i = 0; i < Labels.size (); i++)
    {
        const B& Labels = Labels[i]; //GETTING ERROR HERE
        fprintf (file,"%dn",
                i + 1,
                Labels.timestamp.toString ("%H:%M:%S").c_str (),
    }
}

我得到一个错误

error: no match for ‘operator[]’ in ‘Labels[i]’

您在这里隐藏了B的名称:

const B& Labels = Labels[i];

当您这样做时,RHS上的Labelsconst B&,而这没有operator[]

你需要选择一个不同的名字:

const B& foobar = Labels[i];
相关文章: