下面的代碼片段是一個遊戲,編譯器抱怨返回值,所以我想要一些關於如何以其他方式做這個技巧的反饋,讓一個函數根據放入的類型返回兩種不同的類型但沒有超載返回多種類型
template <typename T>
T GetTimeDead(uint64 Guid)
{
bool stringOutput;
if(typeid(T) == typeid(float))
stringOutput = false;
else
stringOutput = true;
bool found = false;
for(map<uint32, TrackInfo>::iterator itr = dieTracker.begin(); itr != dieTracker.end(); ++itr)
{
if(itr->second.GUID == Guid)
{
found = true;
break;
}
}
if(!found)
stringOutput ? return "never" : return sObjectMgr->FindCreature(Guid)->GetCreatureData()->spawntimesecs;
if(!stringOutput)
return dieTracker.find(Guid)->second.seconds;
float seconds = dieTracker.find(Guid)->second.seconds;
uint64 secs = seconds % 60;
uint64 minutes = seconds % 3600/60;
uint64 hours = seconds % 86400/3600;
uint64 days = seconds/86400;
ostringstream ss;
if(days)
days != 1 ? ss << days << " Days " : ss << days << " Day ";
if(hours)
hours != 1 ? ss << hours << " Hours" : ss << hours << " Hour";
if(minutes)
minutes != 1 ? ss << minutes << " Minutes " : ss << minutes << " Minutes ";
if(secs || (!days && !hours && !minutes))
secs != 1 ? ss << secs << " Seconds " : ss << secs << " Second ";
ss << "ago";
return ss.str();
}
什麼是錯誤? – 2013-05-11 19:35:21
錯誤C2059:語法錯誤:'return' – user2373581 2013-05-11 19:37:12
它聽起來像編譯器是正確的:你正在嘗試做一件壞事。你可以添加一個解釋你準備完成什麼? – Elazar 2013-05-11 19:37:37