Cannot Deduce Argument for Type 'T'?  
Author Message
Sweeps78





PostPosted: Visual C++ General, Cannot Deduce Argument for Type 'T'? Top

Could anyone tell me why:

List<RobotPart^>^ test = s->FlatPartList();

results in the following error:

Error 1 error C2783: 'System::Collections::Generic::List<T> ^TrossenRoboticsSystem::TROM::RobotPart::FlatPartList(void)' : could not deduce generic argument for 'T' c:\alex\flashdrive\projects\trossen robotics system\projects\demo navigation\demo navigation cpp\Form1.h 328

FlatPartList() is a method in a referenced C# DLL. Here's the code for FlatPartList():

public List<T> FlatPartList<T>() where T : RobotPart
{
List<T> part = new List<T>();

if (this is T)
{
part.Add((T)this);
}

//Recurse down the children.
foreach (Socket socket in Sockets)
{
foreach (RobotPart childPart in socket.RobotParts)
{
//But NOT inside other robots
if (!(childPart is Robot))
{
////Add each part we found to the list
List<T> childParts = childPart.FlatPartList<T>();
foreach (T grandchildPart in childParts)
{
part.Add(grandchildPart);
}
}
else
{
Type t = typeof(T);
if (t == typeof(Robot) && (!(childPart is Appendage)))
{
part.Add((T)childPart);
}
else if (t == typeof(Appendage) && ((childPart is Appendage)))
{
part.Add((T)childPart);
}
}
}
}

return part;
}




Visual C++3  
 
 
Mike Danes





PostPosted: Visual C++ General, Cannot Deduce Argument for Type 'T'? Top

Because template/generic arguments cannot be deduced from the return type, they can only be deduced from function arguments. You need to specify the generic argumen type:

List<RobotPart^>^ test = s->FlatPartList<RobotPart^>();


 
 
Sweeps78





PostPosted: Visual C++ General, Cannot Deduce Argument for Type 'T'? Top

Wow, am i embarrassed! I definitely should have caught that!

Thanks alot Mike:)