C++程序设计语言第十二章 “图形系统设计”中的intersect(Shape*Shape*)多重指派虚机制 VC6提示include/xtree出错,怎么处理
C++程序设计语言第十二章 “图形系统设计”中的intersect(Shape*,Shape*)多重指派虚机制 VC6提示include/xtree出错,怎么办
这个图形库设计是《C++程序设计语言 特别版》第十二章的习题12.2,之后在习题12.10中要求读者编写一个多重指派的虚机制来实现intersect(Shape*,Shape*)碰撞检测函数(如图,并不需要编写实际的检测代码,只需要保证能运行正确的函数)。我自己设计了一个比较合理的实现方法,用map容器保存Shape的typeinfo运行时类型信息,然后绑定到一个碰撞检测函数上。运行时从Map中找出对应检测函数。以下是具体实现:
代码完全正确,实际连接时VC6报了一大堆错误:
--------------------Configuration: CGraphics - Win32 Debug--------------------
Compiling...
main.cpp
e:\visual c++ 6.0\include\xtree(183) : warning C4786: 'std::_Tree<std::pair<type_info const *,type_info const *>,std::pair<std::pair<type_info const *,type_info const *> const ,bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *)>,std::map<std::pa
ir<type_info const *,type_info const *>,bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *),Cgraphics::TwoTypesOrder,std::allocator<bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *)> >::_Kfn,Cgraphics::TwoTypesOrder,std::allocator<bool (__cd
ecl*)(Cgraphics::Shape *,Cgraphics::Shape *)> >::_Redbl' : identifier was truncated to '255' characters in the debug information
e:\visual c++ 6.0\include\xtree(174) : while compiling class-template member function '__thiscall std::_Tree<std::pair<type_info const *,type_info const *>,std::pair<std::pair<type_info const *,type_info const *> const ,bool (__cdecl*)(Cgrap
hics::Shape *,Cgraphics::Shape *)>,std::map<std::pair<type_info const *,type_info const *>,bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *),Cgraphics::TwoTypesOrder,std::allocator<bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *)> >::_Kfn
,Cgraphics::TwoTypesOrder,std::allocator<bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *)> >::~std::_Tree<std::pair<type_info const *,type_info const *>,std::pair<std::pair<type_info const *,type_info const *> const ,bool (__cdecl*)(Cgraphics:
:Shape *,Cgraphics::Shape *)>,std::map<std::pair<type_info const *,type_info const *>,bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *),Cgraphics::TwoTypesOrder,std::allocator<bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *)> >::_Kfn,Cgra
这个图形库设计是《C++程序设计语言 特别版》第十二章的习题12.2,之后在习题12.10中要求读者编写一个多重指派的虚机制来实现intersect(Shape*,Shape*)碰撞检测函数(如图,并不需要编写实际的检测代码,只需要保证能运行正确的函数)。我自己设计了一个比较合理的实现方法,用map容器保存Shape的typeinfo运行时类型信息,然后绑定到一个碰撞检测函数上。运行时从Map中找出对应检测函数。以下是具体实现:
- C/C++ code
typedef std::pair<const std::type_info*,const std::type_info*> TwoTypes; typedef bool (*Intersector)(Shape*,Shape*); struct TwoTypesOrder{ bool operator()(const TwoTypes& lhs,const TwoTypes& rhs) { if(lhs.first->before(*rhs.first)) return true; else if(*lhs.first==*rhs.first) return lhs.second->before(*rhs.second); else return false; } }; typedef std::map<TwoTypes,Intersector,TwoTypesOrder> DispatchMap; DispatchMap& intersector_map() { static DispatchMap dispatch_table; return dispatch_table; } template<class Type1,class Type2> void register_intersector(Intersector pf) { intersector_map().insert( std::make_pair(&typeid(Type1),&typeid(Type2)),pf); } bool intersect(Shape* lhs,Shape* rhs) { DispatchMap::iterator p= intersector_map().find(std::make_pair(&typeid(*lhs),&typeid(*rhs))); return (*p->second)(lhs,rhs); } bool circles_intersect(Shape* lhs,Shape* rhs) { Circle* pc1=dynamic_cast<Circle*>(lhs); Circle* pc2=dynamic_cast<Circle*>(rhs); RECT r={0,0,800,600}; drawtext("circles_intersect",&r,DT_CENTER|DT_VCENTER|DT_SINGLELINE); return true; } class Window{ public: Window(int width,int height) { initgraph(width,height); register_all_intersectors(); } ~Window(){closegraph();} private: void register_all_intersectors() { register_intersector<Circle,Circle>(circles_intersect); } };
代码完全正确,实际连接时VC6报了一大堆错误:
--------------------Configuration: CGraphics - Win32 Debug--------------------
Compiling...
main.cpp
e:\visual c++ 6.0\include\xtree(183) : warning C4786: 'std::_Tree<std::pair<type_info const *,type_info const *>,std::pair<std::pair<type_info const *,type_info const *> const ,bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *)>,std::map<std::pa
ir<type_info const *,type_info const *>,bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *),Cgraphics::TwoTypesOrder,std::allocator<bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *)> >::_Kfn,Cgraphics::TwoTypesOrder,std::allocator<bool (__cd
ecl*)(Cgraphics::Shape *,Cgraphics::Shape *)> >::_Redbl' : identifier was truncated to '255' characters in the debug information
e:\visual c++ 6.0\include\xtree(174) : while compiling class-template member function '__thiscall std::_Tree<std::pair<type_info const *,type_info const *>,std::pair<std::pair<type_info const *,type_info const *> const ,bool (__cdecl*)(Cgrap
hics::Shape *,Cgraphics::Shape *)>,std::map<std::pair<type_info const *,type_info const *>,bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *),Cgraphics::TwoTypesOrder,std::allocator<bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *)> >::_Kfn
,Cgraphics::TwoTypesOrder,std::allocator<bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *)> >::~std::_Tree<std::pair<type_info const *,type_info const *>,std::pair<std::pair<type_info const *,type_info const *> const ,bool (__cdecl*)(Cgraphics:
:Shape *,Cgraphics::Shape *)>,std::map<std::pair<type_info const *,type_info const *>,bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *),Cgraphics::TwoTypesOrder,std::allocator<bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *)> >::_Kfn,Cgra