Template function calling method of object of templated type

753 Views Asked by At

I've got a function that I am trying to template so I can reuse it with a few different object types that all share the same method names for getting/setting information. I am still learning about templates, this is my first real attempt at using them to try and eliminate duplicate code, and I think I must be doing something illegal or incorrect.

Here's my function, cut down to the first line of code where I am getting hung up:

//
// Function - AddObjectToListboxAndMap: This function adds a newly created templated objected to the given map and listbox after uniquely naming it, then selects it
//
template <typename T>
void AddObjectToListboxAndMap(ListBox^ lbObject, std::map<std::string, T>* tmap, T tobj)
{
    // Uniquely name object
    tobj->SetName("new object" + boost::lexical_cast<std::string>(tobj->(GetGUID())));

    // More code here...
}

I am using 4 different std::map objects:

std::map<std::string, Area*>* listbox_to_world_area_map;
std::map<std::string, Room*>* listbox_to_area_rooms_map;
std::map<std::string, Mob*>* listbox_to_area_mobs_map;
std::map<std::string, Item*>* listbox_to_area_items_map;

And this is how I am calling the function:

// "area" previously initialized...
Room* r = area->CreateAndAddRoom();
AddObjectToListboxAndMap<Room*>(lbEditRooms, listbox_to_area_rooms_map, r);

When the first line in the AddObjectToListboxAndMap function is called, I get a syntax error: Error 1 error C2059: syntax error : '('

After some digging, I'm feeling like the problem is the compiler not knowing how to call a method of an object of a templated type. Do I need to start messing around with function pointers to get this working, or am I missing something simple? I was hoping to just have the compiler throw an error if code elsewhere called the function and passed it an object that doesn't have a "SetName" method, but it may not work this way. Any ideas on how to properly approach this?

0

There are 0 best solutions below