Skip to main content
gamedev.net
🔒 Locked

Weird constructors error

Started by lougv22 Apr 9, 2009 at 2:58 PM 5 replies 600+ views
Original Post
lougv22
lougv22
I have two constructors declared in a class, differing in the type of arguments: #ifndef _STAR_TASKEVENT_H_ #define _STAR_TASKEVENT_H_ class MCLARINSHARED_API TaskEvent: public Event { public: TaskEvent(ObjectID ObjectID, MessageID messageID, CompMap object); TaskEvent(ObjectID objectID, MessageID messageID, TaskReportGameStruct& taskReport); } The only difference between the two constructors is in the type of the 3rd parameter, In the first constructor it is of type CompMap and in the second one of type TaskReportGameStruct. So in another class I want to call the second constructor like so: for (TaskReportGameStructList::const_iterator taskReportIterator = taskReportList->begin(); taskReportIterator != taskReportList->end(); ++taskReportIterator) { TaskEvent* taskEvent = new TaskEvent((*taskReportIterator)->getObjectID(), TaskEvent::MESSAGE_CLIENT_NEW, (*taskReportIterator)); } // End for But it seems like it thinks I want to call the first one because I get this error: error C2664: cannot convert parameter 3 from 'Star::TaskReportGameStruct *const ' to 'Star::TaskEvent::CompMap' Why would that be happening? Clearly I am telling it to call the 2nd constructors because I am passing as a parameter an object of type TaskReportGameStruct, and for some reason it is asking for a parameter of type CompMap.
SiCrane
SiCrane
What's TaskReportGameStructList?
lougv22
lougv22
It's a vector of pointers to TaskReportGameStruct objects defined like so:

typedef std::vector TaskReportGameStructList;
SiCrane
SiCrane
If you have a TaskReportGameStructList::const_iterator and dereference it once, you get a TaskReportGameStruct*, not a TaskReportGameStruct &. If you want a TaskReportGameStruct & you need to dereference it twice.
lougv22
lougv22
hmm, yeah, that worked. Thanks! So an iterator is in effect a pointer to an object it iterates through then?
SiCrane
SiCrane
The iterator interface was modeled on pointers. std::list<>::iterator is a bidirectional iterator so it supports most pointer operations, but lacks things like iterator arithmetic and operator[].
Zahlman
Zahlman
Quote:
Original post by lougv22
hmm, yeah, that worked. Thanks! So an iterator is in effect a pointer to an object it iterates through then?


Some would rather state it thus: a pointer is in effect an iterator (specifically a random-access one) over an array, whenever it actually points into an array. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.