Having trouble with this program complex.h

  • Thread starter Thread starter FrostScYthe
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to the use of the complex number library in C++. Participants are addressing a specific error encountered in the code involving the `cabs` function and its compatibility with complex numbers. The scope includes technical explanations and suggestions for resolving the error.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports an error stating "no matching function for call to `cabs(std::complex&)`" when trying to use the `cabs` function with complex numbers.
  • Another participant suggests checking if `cabs` is in the `std` namespace and proposes using `std::abs` instead, questioning the portability and standard compliance of `cabs`.
  • A third participant mentions that MSVC++ is not ISO compliant and recommends checking the documentation for the Visual Studio distribution that comes with MSVC++.
  • Another participant clarifies that `complex.h` and `complex` are unrelated, stating that `cabs` is a C99 function while C++ overloads the `abs` function for complex numbers.
  • It is noted that if a `using namespace std` command has already been issued, it is redundant to issue a `using std::complex` command.

Areas of Agreement / Disagreement

Participants express differing views on the appropriateness of using `cabs` versus `abs`, and there is no consensus on the best approach to resolve the error. The discussion remains unresolved regarding the implications of using MSVC++ and its compliance with standards.

Contextual Notes

Limitations include potential misunderstandings about the compatibility of C99 and C++ libraries, as well as the implications of using specific functions based on the compiler being used.

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;
count << "Type in a complex number: ";
cin >> c1;
c1 *= 2;
count << "|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:

Similar threads

  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
2
Views
2K
  • · Replies 39 ·
2
Replies
39
Views
5K
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
3
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
7K
  • · Replies 40 ·
2
Replies
40
Views
4K
Replies
12
Views
3K