Thursday, 15 July 2010

c++ - error C2664: cannot convert parameter 1 from 'X' to 'X' -



c++ - error C2664: cannot convert parameter 1 from 'X' to 'X' -

i have c++/win32/mfc project in visual studio 2008, , i'm getting unusual error message when compile it.

i've created little project demonstrate problem, , main code is

#ifndef _myobject_h #define _myobject_h class myobject { public: myobject() { } }; #endif // _myobject_h // --- end myobject.h // --- begin objectdata.h #ifndef _objectdata_h #define _objectdata_h template <typename datapolicy> class objectdata { public: datapolicy *data; objectdata() : data(null) { } objectdata(const objectdata<datapolicy> &copy) : data(copy.data) { } objectdata<datapolicy> & operator=(const objectdata<datapolicy> &copy) { this->data = copy.data; homecoming *this; } }; #endif // _objectdata_h // --- end objectdata.h // --- begin tool.h #ifndef _tool_h #define _tool_h #include "objectdata.h" template <typename objectpolicy> class tool { private: objectdata<typename objectpolicy> _object; public: tool(objectdata<typename objectpolicy> obj); }; #endif // _tool_h // --- end tool.h // --- begin tool.cpp #include "stdafx.h" #include "tool.h" template <typename objectpolicy> tool<objectpolicy>::tool(objectdata<typename objectpolicy> obj) : _object(obj) { } // --- end tool.cpp // --- begin engine.h #ifndef _engine_h #define _engine_h #include "tool.h" #include "myobject.h" class engine { private: myobject *_obj; public: engine(); ~engine(); void dosomething(); }; #endif // _engine_h // --- end engine.h // --- begin engine.cpp #include "stdafx.h" #include "engine.h" engine::engine() { this->_obj = new myobject(); } engine::~engine() { delete this->_obj; } void engine::dosomething() { objectdata<myobject> objdata; objdata.data = this->_obj; // next line error occurs tool< objectdata<myobject> > *tool = new tool< objectdata<myobject> >(objdata); } // --- end engine.cpp

errors:

engine.cpp c:\projects\myproject\myproject\engine.cpp(18) : error c2664: 'tool::tool(objectdata)' : cannot convert parameter 1 'objectdata' 'objectdata' with [ objectpolicy=objectdata, datapolicy=objectdata ] and [ datapolicy=myobject ] and [ datapolicy=objectdata ] no user-defined-conversion operator available can perform conversion, or operator cannot called 1>build log saved @ "file://c:\projects\myproject\myproject\debug\buildlog.htm" myproject - 1 error(s), 0 warning(s)

thanks help.

there few problems code. first of all, using typename keyword in wrong way. typename can used when qualified type names used (and required when type names dependent), not case:

template <typename objectpolicy> class tool { private: objectdata<typename objectpolicy> _object; // "typename" not needed! public: tool(objectdata<typename objectpolicy> obj); // "typename" not needed! };

the problem complain about, however, in instantiation of tool class template:

tool< objectdata<myobject> > *tool = new tool< objectdata<myobject> >(objdata);

your tool<> template contains fellow member variable of type objectdata<objectpolicy>, objectpolicy class template parameter. however, in line above instantiate tool objectdata<myobject> parameter. means fellow member variable have type objectdata<objectdata<myobject>>, , type of constructor's parameter.

because of this, trying invoke constructor accepts objectdata<objectdata<myobject>> argument of mismatching type objectdata<myobject>. hence, error get.

you should alter instantiation into:

tool< myobject > *tool = new tool< myobject >(objdata);

another problem have definition of tool's fellow member functions in separate .cpp files. should not that: linker won't able see when processing separate translation unit.

to solve problem, set definitions of class template's fellow member functions same header class template defined (tool.h in case).

c++ visual-studio-2008

No comments:

Post a Comment