C++ 在对象外部的唯一_ptr集合中循环

C++ 在对象外部的唯一_ptr集合中循环,c++,c++11,unique-ptr,C++,C++11,Unique Ptr,我试图通过让类返回一个vector::iterator,从类外部循环遍历对象Baz内部的指针集合。当我运行for循环时,我得到以下错误: “const class std::unique\u ptr”没有名为“getName”的成员。 有人能解释一下发生了什么,以及我如何能够在baz内部的唯一指针集合中进行循环吗?提前谢谢 #include <iostream> #include <string> #include <memory> #include <

我试图通过让类返回一个
vector::iterator
,从类外部循环遍历对象
Baz
内部的指针集合。当我运行
for
循环时,我得到以下错误:

“const class std::unique\u ptr”没有名为“getName”的成员。

有人能解释一下发生了什么,以及我如何能够在baz内部的唯一指针集合中进行循环吗?提前谢谢

#include <iostream>
#include <string>
#include <memory>
#include <vector>

class BaseInterface
{
protected:

std::string Name;

public:

virtual ~BaseInterface(){}
virtual void setName( std::string ObjName ) = 0;
virtual std::string getName() = 0;
};

class Foo : public BaseInterface
{
public:

void setName( std::string ObjName )
{
    Name = ObjName;
}

std::string getName()
{
    return Name;
}

};

class Bar : public BaseInterface
{
public:

void setName( std::string ObjName )
{
    Name = ObjName;
}

std::string getName()
{
    return Name;
}

};

class Baz
{
    protected:

    std::vector< std::unique_ptr< BaseInterface > > PointerList;

    public:

    void push_back( std::unique_ptr< BaseInterface > Object )
    {
        PointerList.push_back( std::move( Object ) );
    }

    std::vector< std::unique_ptr< BaseInterface > >::const_iterator begin()
    {
        return PointerList.begin();
    }

    std::vector< std::unique_ptr< BaseInterface > >::const_iterator end()
    {
        return PointerList.end();
    }

};

int main( int argc, char ** argv )
{

std::unique_ptr< BaseInterface > FooObj( new Foo() );
FooObj->setName( "Foo" );

std::unique_ptr< BaseInterface > BarObj( new Bar() );
BarObj->setName( "Bar" );

std::unique_ptr< Baz > BazObj( new Baz() );

BazObj->push_back( std::move( FooObj ) );
BazObj->push_back( std::move( BarObj ) );

for( auto it = BazObj->begin(); it != BazObj->end(); ++it )
{
    std::cout << "This object's name is " << it->getName() << std::endl;
}

return 0;
}
#包括
#包括
#包括
#包括
类基类接口
{
受保护的:
std::字符串名;
公众:
虚拟~BaseInterface(){}
虚拟void setName(std::string ObjName)=0;
虚拟std::string getName()=0;
};
类Foo:公共基接口
{
公众:
void setName(std::string ObjName)
{
名称=ObjName;
}
std::string getName()
{
返回名称;
}
};
类栏:公共BaseInterface
{
公众:
void setName(std::string ObjName)
{
名称=ObjName;
}
std::string getName()
{
返回名称;
}
};
Baz类
{
受保护的:
std::vector>指针列表;
公众:
void push_back(std::unique_ptrObject)
{
指针列表。向后推(std::move(Object));
}
std::vector>::const\u迭代器begin()
{
返回指针列表。begin();
}
std::vector>::const\u迭代器end()
{
返回指针list.end();
}
};
int main(int argc,字符**argv)
{
std::unique_ptrFooObj(新的Foo());
FooObj->setName(“Foo”);
std::unique_ptrBarObj(新条());
BarObj->setName(“Bar”);
std::unique_ptrBazObj(新Baz());
BazObj->push_back(标准::移动(FooObj));
BazObj->push_back(标准::移动(BarObj));
用于(自动it=BazObj->begin();it!=BazObj->end();++it)
{

std::cout您应该首先取消对迭代器的引用:

std::cout << "This object's name is " << (*it)->getName() << std::endl;
//                                       ^^^^^

std::我也试过了吗*但我想我忘记了括号。谢谢你指出我的错误。一个小的跟进,但我认为
->
会取消
对它的引用。这有时让我很困惑,因为有时只需要一个
*
。@Tek:A
unique\u ptr
的迭代器是“like”一个双指针,因此您必须取消引用它两次:一个从迭代器到容器中存储的
unique\u ptr
,另一个从
unique\u ptr
到它所指的实际对象。@MatteoItalia谢谢,我通常不迭代指针集合,所以解释对可视化有很大帮助使流动更加顺畅。