2012-11-22 49 views
1

我在C++以下功能:C++未知错误

std::wstring decToDeg(double input) 
{ 
    int deg, min; 
    double sec, sec_all, min_all; 
    std::wstring output; 

    sec_all = input * 3600; 
    sec = Math::Round(static_cast<int>(sec_all) % 60, 3); //code from @spin_eight answer 
    min_all = (sec_all - sec)/60; 
    min = static_cast<int>(min_all) % 60; 
    deg = static_cast<int>(min_all - min)/60; 
    output = deg + L"º " + min + L"' " + sec + L"\""; 

    return output; 
} 

当我尝试编译我得到这个错误:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion) 

我能做些什么来纠正我的功能这两个错误?

编辑:解决

std::wstring decToDeg(double input) 
{ 
    int deg, min; 
    double sec, sec_all, min_all; 

    sec_all = input * 3600; 
    sec = Math::Round(static_cast<int>(sec_all) % 60, 3); 
    min_all = (sec_all - sec)/60; 
    min = static_cast<int>(min_all) % 60; 
    deg = static_cast<int>(min_all - min)/60; 

    std::wostringstream output; 
    output << deg << L"º " << min << L"' " << sec << L"\""; 

    return output.str(); 
} 
+1

'sec_all'是'double',该错误信息是很清楚的。 –

+0

此外,你正试图总结数字与字符串,这是不能这样做在C + + –

+0

你能告诉我该怎么做来解决这些错误?我完全是新的C++ ... – Victor

回答

1

你可以使用一个字符串流来构造output,像这样:

std::wostringstream output; 
output << deg << L"º " << min << L"' " << sec << L"\""; 

return output.str(); 
+1

该程序编译失败... – Victor

+0

@Victor什么错误?顺便说一句,当然,你必须删除你的原始声明'output'作为'wstring',你必须'#include ' – Angew

+0

我终于开始工作了,谢谢 – Victor

1
sec = Math::Round(static_cast<int>(sec_all) % 60, 3); 
+0

它解决了我的一个错误,但并不完整 – Victor

+0

output = std :: to_string(deg)+ L“º”+ std :: to_string(min)+ L“'”+ std :: to_string(sec)+ L“\”“; –

1

不能对双打使用模。 模对双打:

int result = static_cast<int>(a/b); 
return a - static_cast<double>(result) * b; 
0

对于第一个错误试试这个:

min = (static_cast<int>(min_all)) % 60; 

这应该确保广播将被首先完成,试图做任何其他的计算之前。

如果您的其他错误不是由第一个错误引起的错误,那么您可能需要尝试使用stringstream。它的行为就像一个普通的I/O流,因此非常适合格式化字符串。