Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Having trouble with this program complex.h

  1. Sep 15, 2006 #1
    Hey, I'm having trouble with this program

    This is the code:

    #include <complex>
    #include <iostream>

    using namespace std;
    using std::complex;

    int main ()
    complex <double> c1; // complex numbers with double components
    complex <double> c2;
    cout << "Type in a complex number: ";
    cin >> c1;
    c1 *= 2;
    cout << "|c1*2| = " << cabs(c1) << endl;
    return 0;

    however it doesn't seem to recognize any of the complex functions.. I don't know why... ? I get this error

    14 C:\Dev-Cpp\main23.cpp no matching function for call to `cabs(std::complex<double>&)'
  2. jcsd
  3. Sep 16, 2006 #2
    Couple thoughts:
    You give a windows style path, are you using MS Visual C++?
    1. Is cabs in namespace std? Try "std::cabs(...".
    2. Why use cabs? Why not use std::abs?
    3. MSVC++ doc lists _cabs, not cabs. Is cabs/_cabs standard, portable, etc? Not sure, but the abs provided via <complex> looks standard in C++.

    Last edited: Sep 16, 2006
  4. Sep 16, 2006 #3

    jim mcnamara

    User Avatar

    Staff: Mentor

    MSVC++ is not ISO compliant. So you might want to plunge into your mdsn for visual studio distribtuion that comes with MSVC++ and try a look at "complex"
  5. Sep 17, 2006 #4


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    complex.h and complex are unrelated. The C99 and C++ complex libraries are disjoint. cabs is the C99 function. C++ simply overloads the abs function.

    In other words, you wanted to use abs.

    P.S. if you've already issued a using namespace std command, it is redundant to issue a using std::complex command.
    Last edited: Sep 17, 2006
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook