【问题标题】:C++ unknown errorsC++ 未知错误
【发布时间】:2012-11-10 13:25:38
【问题描述】:

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

【问题讨论】:

  • sec_alldouble,错误信息很清楚。
  • 另外,您正在尝试将数字与字符串相加,而这在 C++ 中是无法做到的
  • 您能告诉我如何解决这些错误吗?我对 C++ 完全陌生...
  • 查看stackoverflow.com/questions/64782/… 了解修复#2 的各种方法
  • 这不是 C++。它是 C++/CLI。

标签: syntax compiler-errors c++-cli


【解决方案1】:

您可以使用字符串流来构造output,如下所示:

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

return output.str();

【讨论】:

  • @Victor 什么错误?顺便说一句,当然您必须将output 的原始声明删除为wstring,并且您必须将#include &lt;sstream&gt; 删除。
【解决方案2】:
sec = Math::Round(static_cast<int>(sec_all) % 60, 3);

【讨论】:

  • 输出 = std::to_string(deg) + L"º " +std::to_string(min) + L"' " + std::to_string(sec) + L"\"";
【解决方案3】:

您不能对双精度数使用模数。 对双打取模:

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

【讨论】:

    【解决方案4】:

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

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

    这应该确保先完成演员表,然后再尝试进行任何其他计算。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 2017-07-13
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      相关资源
      最近更新 更多