1

I'm attempting to build source files of an open source C++ library written by someone else. This is being done on Windows with Cygwin's mingw-w64 compiler. The only compiler option I'm attaching is -std=gnu++11 since the library depends on some C++11 features.

Here are some examples of code in their library that appears to be causing issues:

CPScalar & Abs()
{
    m_dValue = std::abs(m_dValue);
    return *this;
}

//...

template<typename Unit>
bool SEScalarQuantity<Unit>::Set(const SEScalarQuantity<Unit>& s)
{
  if (m_readOnly)
    throw CommonDataModelException("Scalar is marked read-only");
  if (!s.IsValid())
    return false;
  m_value = s.m_value;
  m_isnan = (std::isnan(m_value)) ? true : false;
  m_isinf = (std::isinf(m_value)) ? true : false;
  m_unit = s.m_unit;
  return true;
}

I get compiler errors on the std:: qualified functions above. The compiler error on the m_dValue = std::abs(m_dValue); line is

error: call of overloaded 'abs(double&)' is ambiguous

Which made me think it could be related to the question of whether std::abs(0u) is ill-formed as well as this answer to a similar SO question.

m_isnan = (std::isnan(m_value)) ? true : false; and the following line gives me

error: expected unqualified-id before '(' token

There are countless other uses of std:: that the compiler doesn't complain about. If I remove all of the std:: qualifiers in the statements that are giving me errors, the code compiles beautifully.

Thing is, this open source project is (presumably) being built by others without modification, so what am I missing here?

Community
  • 1
  • 1
NanoWizard
  • 2,104
  • 1
  • 21
  • 34
  • If there is `using namespace std;` in the global scope then either delete that and fix all the error or just drop the library and fine something else. More reading: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – NathanOliver Feb 17 '17 at 13:27
  • 2
    @NathanOliver -- while removing `using namespace std;` is good advice, it won't fix this problem – Pete Becker Feb 17 '17 at 13:29
  • One guess is that the code doesn't include ``, which is the *formal* requirement for using the `std::` versions. With some compilers they might be visible anyway. – Bo Persson Feb 17 '17 at 13:34

1 Answers1

4

Add #include <cmath> to the file being compiled. The problem is that there are a couple of overloads of std::abs for integer types that are declared in the header <cstdlib> and the compiler is complaining that it doesn't know which of those to use. What's needed, though, is std::abs(double), and that's declared in <cmath>.

The reason that this code works with some compilers and not others is probably that there is a declaration of std::abs(double) coming in from some header other than <cmath>. That's allowed, but not required.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • This works. I was hoping to avoid modifying any of the library files though, so that I don't have to worry about those modifications when upgrading to a later version – NanoWizard Feb 17 '17 at 13:43