如何通过迭代器的强制转换来解决这个问题?
编译器(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您的dynamic_cast语法是错误的(或者可能是格式问题?):
Dynamic_cast 语法:
dynamic_cast(BaseClass*)
或此代码对我来说看起来非常可疑。你想达到什么目的?为什么要动态强制转换迭代器?这没有任何意义。
编辑
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*)
ORThis 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 yourTypeToThreadIterMapStorage
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 aconst_cast
to remove the constness of the object and notdynamic_cast
. But note that this is a dangerous thing to do.The simpler way is to declare theData as a non-const iterator.
是什么让您认为 TypeData::iterator 和 TypeData::const_iterator 甚至是相关的?
为什么不将“theData”的类型更改为迭代器?
What makes you think that TypeData::iterator and TypeData::const_iterator are even related?
Why not change the type of 'theData' to an iterator?