Having trouble with this program complex.h

  • Thread starter Thread starter FrostScYthe
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
The discussion revolves around a coding issue with a C++ program that uses complex numbers. The user encounters an error stating that there is no matching function for the call to `cabs` with a `std::complex<double>` argument. Key points include the suggestion to use `std::cabs` instead of `cabs`, as well as the recommendation to utilize `std::abs`, which is more appropriate for complex numbers in C++. It is noted that the C++ standard library overloads the `abs` function for complex types, while `cabs` is a C99 function that may not be compatible with C++. Additionally, there is a mention of MSVC++ not being fully ISO compliant, which could affect functionality. Lastly, it is pointed out that using `using namespace std` makes the additional `using std::complex` declaration unnecessary.
FrostScYthe
Messages
80
Reaction score
0
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;
cin.get();
cin.get();
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>&)'
 
Technology news on Phys.org
FrostScYthe said:
14 C:\Dev-Cpp\main23.cpp no matching function for call to `cabs(std::complex<double>&)'

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++.

Cheers,
Tim
 
Last edited:
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"
 
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:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top