读书人

C++程序设计语言第十二章 “图形系统设

发布时间: 2012-05-04 12:36:09 作者: rapoo

C++程序设计语言第十二章 “图形系统设计”中的intersect(Shape*,Shape*)多重指派虚机制 VC6提示include/xtree出错,怎么办

这个图形库设计是《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


phics::TwoTypesOrder,std::allocator<bool (__cdecl*)(Cgraphics::Shape *,Cgraphics::Shape *)> >(void)'
e:\visual c++ 6.0\include\xtree(183) : warning C4786: '_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<ty
pe_info const *,type_info const *>,bool (__cdecl*)(Cgraphics::' : 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<struct std::pair<class type_info const *,class type_info const *>,struct std::pair<struct std::pair<class type_info const *,class ty
pe_info const *> const ,bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)>,struct std::map<struct std::pair<class type_info const *,class type_info const *>,bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *),struct C
graphics::TwoTypesOrder,class std::allocator<bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)> >::_Kfn,struct Cgraphics::TwoTypesOrder,class std::allocator<bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)> >::~std
::_Tree<struct std::pair<class type_info const *,class type_info const *>,struct std::pair<struct std::pair<class type_info const *,class type_info const *> const ,bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)>,struct std::map<s
truct std::pair<class type_info const *,class type_info const *>,bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *),struct Cgraphics::TwoTypesOrder,class std::allocator<bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape
*)> >::_Kfn,struct Cgraphics::TwoTypesOrder,class std::allocator<bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)> >(void)'
e:\visual c++ 6.0\include\xtree(183) : warning C4786: '~_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<t
ype_info const *,type_info const *>,bool (__cdecl*)(Cgraphics:' : identifier was truncated to '255' characters in the debug information
e:\visual c++ 6.0\include\xtree(514) : error C2662: '()' : cannot convert 'this' pointer from 'const struct Cgraphics::TwoTypesOrder' to 'struct Cgraphics::TwoTypesOrder &'
Conversion loses qualifiers
e:\visual c++ 6.0\include\xtree(511) : while compiling class-template member function 'struct std::_Tree<struct std::pair<class type_info const *,class type_info const *>,struct std::pair<struct std::pair<class type_info const *,class type_i
nfo const *> const ,bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)>,struct std::map<struct std::pair<class type_info const *,class type_info const *>,bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *),struct Cgrap
hics::TwoTypesOrder,class std::allocator<bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)> >::_Kfn,struct Cgraphics::TwoTypesOrder,class std::allocator<bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)> >::_Node *_
_thiscall std::_Tree<struct std::pair<class type_info const *,class type_info const *>,struct std::pair<struct std::pair<class type_info const *,class type_info const *> const ,bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)>,stru
ct std::map<struct std::pair<class type_info const *,class type_info const *>,bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *),struct Cgraphics::TwoTypesOrder,class std::allocator<bool (__cdecl*)(class Cgraphics::Shape *,class Cgra
phics::Shape *)> >::_Kfn,struct Cgraphics::TwoTypesOrder,class std::allocator<bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)> >::_Lbound(const struct std::pair<class type_info const *,class type_info const *> &) const'


e:\visual c++ 6.0\include\xtree(514) : error C2064: term does not evaluate to a function
e:\visual c++ 6.0\include\xtree(511) : while compiling class-template member function 'struct std::_Tree<struct std::pair<class type_info const *,class type_info const *>,struct std::pair<struct std::pair<class type_info const *,class type_i
nfo const *> const ,bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)>,struct std::map<struct std::pair<class type_info const *,class type_info const *>,bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *),struct Cgrap
hics::TwoTypesOrder,class std::allocator<bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)> >::_Kfn,struct Cgraphics::TwoTypesOrder,class std::allocator<bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)> >::_Node *_
_thiscall std::_Tree<struct std::pair<class type_info const *,class type_info const *>,struct std::pair<struct std::pair<class type_info const *,class type_info const *> const ,bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)>,stru
ct std::map<struct std::pair<class type_info const *,class type_info const *>,bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *),struct Cgraphics::TwoTypesOrder,class std::allocator<bool (__cdecl*)(class Cgraphics::Shape *,class Cgra
phics::Shape *)> >::_Kfn,struct Cgraphics::TwoTypesOrder,class std::allocator<bool (__cdecl*)(class Cgraphics::Shape *,class Cgraphics::Shape *)> >::_Lbound(const struct std::pair<class type_info const *,class type_info const *> &) const'
执行 cl.exe 时出错.

CGraphics.exe - 1 error(s), 0 warning(s)

我仔细看了一下连接信息,error就两个:
e:\visual c++ 6.0\include\xtree(514) : error C2662: '()' : cannot convert 'this' pointer from 'const struct Cgraphics::TwoTypesOrder' to 'struct Cgraphics::TwoTypesOrder &'
Conversion loses qualifiers
e:\visual c++ 6.0\include\xtree(514) : error C2064: term does not evaluate to a function
但是我还是一头雾水啊。难道是VC6的Include/xtree文件出错了?VC6 根本就没有告诉我我的代码哪里有错啊。我该怎么办啊?希望大家能帮助,谢谢了。

[解决办法]
VC6.0对STL不支持...放到vs 2008(xp)或vs 2010(win7)就正常了...
[解决办法]
turbo c都要30年前的古董了吧。编程不是考古。
你这个图形库活不活都没什么意义的。学点新的东西吧,与时俱进啊。

读书人网 >C++

热点推荐