如何通过迭代器的强制转换来解决这个问题?

发布于 2024-08-08 22:10:01 字数 986 浏览 1 评论 0原文

编译器(VC8)错误是:
错误 C2680:'std::_Tree<_Traits>::iterator':dynamic_cast 的目标类型无效
模拟错误的源代码:
[编辑]源代码现已修复

#include <map>
#include <string>

struct tag_data
{
    int in;
    int on;
    std::string sn;
};

class myclass
{
private:
    typedef std::map<std::string, tag_data> TypeData; 
    TypeData MapStorage;
    typedef std::map<unsigned long, TypeData::iterator > TypeToThreadIterMapStorage; 
    TypeToThreadIterMapStorage ThreadIterMapStorage;

public:
    bool find( std::string& k) 
    {
        TypeData::const_iterator theData ; 
        theData = MapStorage.find(k);
        //ThreadIterMapStorage [ 0 ] = MapStorage.begin();// this is ok
        ThreadIterMapStorage [ 1 ] = dynamic_cast<TypeData::iterator>(theData); // the error occurs here
        return theData != MapStorage.end();
    }

    virtual ~myclass(){}
};

int _tmain(int argc, _TCHAR* argv[])
{
    myclass mc;
    return 0;
}

The compiler (VC8) error is:
error C2680: 'std::_Tree<_Traits>::iterator' : invalid target type for dynamic_cast

the source code to simulate the error :
[EDIT]source fixed now

#include <map>
#include <string>

struct tag_data
{
    int in;
    int on;
    std::string sn;
};

class myclass
{
private:
    typedef std::map<std::string, tag_data> TypeData; 
    TypeData MapStorage;
    typedef std::map<unsigned long, TypeData::iterator > TypeToThreadIterMapStorage; 
    TypeToThreadIterMapStorage ThreadIterMapStorage;

public:
    bool find( std::string& k) 
    {
        TypeData::const_iterator theData ; 
        theData = MapStorage.find(k);
        //ThreadIterMapStorage [ 0 ] = MapStorage.begin();// this is ok
        ThreadIterMapStorage [ 1 ] = dynamic_cast<TypeData::iterator>(theData); // the error occurs here
        return theData != MapStorage.end();
    }

    virtual ~myclass(){}
};

int _tmain(int argc, _TCHAR* argv[])
{
    myclass mc;
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2024-08-15 22:10:01

首先,您的dynamic_cast语法是错误的(或者可能是格式问题?):
Dynamic_cast 语法:dynamic_cast(BaseClass*)

dynamic_cast<DerivedClassNameReference>(BaseClassReference)

此代码对我来说看起来非常可疑。你想达到什么目的?为什么要动态强制转换迭代器?这没有任何意义。

编辑
map 的 begin() 有两个重载,一个返回 const_iterator,另一个返回非 const_iterator。在您注释的代码中,由于您的 TypeToThreadIterMapStorage 映射的 value_type 是非常量迭代器,因此使用 begin() 的第二个版本。但是,theData iteartor 的类型是 const_iterator,并且编译器抱怨 const_iterator 无法转换为非常量迭代器。您需要的是一个 const_cast 来删除对象的常量,而不是 dynamic_cast。但请注意,这是一件危险的事情。

更简单的方法是将 theData 声明为非常量迭代器。

First of all your dynamic_cast syntax is wrong (or may be formatting issue?):
dynamic_cast syntax: dynamic_cast<DerivedClassName*>(BaseClass*) OR

dynamic_cast<DerivedClassNameReference>(BaseClassReference)

This code looks very suspcious to me. What are you trying to achieve? Why do you want to dynamic_cast an iterator? That doesn't make any sense.

EDIT
map's begin() has two overloads, one which returns the const_iterator and the other which returns a non const_iterator. In your commented code, since your TypeToThreadIterMapStorage map's value_type is a non-const iterator the second version of begin() is used. However, the type of theData iteartor is a const_iterator and the compiler is complaining that const_iterator can not be converted into a non-const iterator. What you require is a const_cast to remove the constness of the object and not dynamic_cast. But note that this is a dangerous thing to do.

The simpler way is to declare theData as a non-const iterator.

深海不蓝 2024-08-15 22:10:01

是什么让您认为 TypeData::iterator 和 TypeData::const_iterator 甚至是相关的?

为什么不将“theData”的类型更改为迭代器?

TypeData::iterator theData = MapStorage.find(k);

What makes you think that TypeData::iterator and TypeData::const_iterator are even related?

Why not change the type of 'theData' to an iterator?

TypeData::iterator theData = MapStorage.find(k);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文