Having trouble with this program complex.h

  • Thread starter FrostScYthe
  • Start date
  • Tags
    Program
In summary, a conversation is taking place about a program that is having trouble recognizing complex functions. The conversation suggests using the std::abs function instead of cabs, and also mentions that MSVC++ is not ISO compliant. It is recommended to look into the "complex"complex.h file for more information. The conversation also mentions that using namespace std and using std::complex commands may be redundant if both have already been used.
  • #1
FrostScYthe
80
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
  • #2
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:
  • #3
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"
 
  • #4
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:

1. What is the purpose of the program complex.h?

The program complex.h is a header file that contains functions and definitions related to complex numbers. It is used in programming to perform operations on complex numbers, such as addition, subtraction, multiplication, and division.

2. What are some common errors that can occur while working with complex.h?

Some common errors that can occur while working with complex.h include syntax errors, type mismatches, and incorrect use of functions. It is important to carefully read the documentation and double-check the code for any mistakes.

3. How can I troubleshoot errors with complex.h?

If you encounter errors with complex.h, the first step is to carefully read the error message to identify the source of the problem. Then, review your code to see if there are any mistakes. You can also refer to the documentation or online resources for troubleshooting tips.

4. Are there any alternative header files to complex.h?

Yes, there are alternative header files that can perform operations on complex numbers, such as cmath and complex. However, complex.h is a widely used and standardized header file that is recommended for working with complex numbers in programming.

5. Can I use complex.h in any programming language?

No, complex.h is a C header file and is not compatible with all programming languages. However, there may be similar header files available for other languages that perform operations on complex numbers. It is important to check the documentation for the programming language you are using to see if there is a compatible header file.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
2
Views
866
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
6
Views
902
  • Programming and Computer Science
Replies
4
Views
777
  • Programming and Computer Science
Replies
3
Views
721
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
40
Views
2K
Back
Top