1

私は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)  

関数でこれら 2 つのエラーを修正するにはどうすればよいですか?

編集:解決しました

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();
}
4

4 に答える 4

1
sec = Math::Round(static_cast<int>(sec_all) % 60, 3);
于 2012-11-22T07:42:32.063 に答える
1

double に対して modulo を使用することはできません。double のモジュロ:

 int result = static_cast<int>( a / b );
 return a - static_cast<double>( result ) * b;
于 2012-11-22T07:44:56.583 に答える
1

次のように、文字列ストリームを使用して を構築できますoutput

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

return output.str();
于 2012-11-22T07:48:21.873 に答える
0

最初のエラーについては、これを試してください:

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

これにより、他の計算を行う前に、キャストが最初に完了するようになります。

他のエラーが最初のエラーによって引き起こされたエラーでない場合は、stringstreamを使用してみてください。通常の I/O ストリームのように動作するため、文字列の書式設定に最適です。

于 2012-11-22T07:45:27.427 に答える