CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2008
    Posts
    2

    Angry compiler errors using complex<float> in C++

    I usually work with complex data. When they changed the complex data class to use templates, they broke most of the code I had written until then. I was able to fix errors like in line 1 by making sure the constant was real, instead of an integer, as in line 2. An annoyance, but not too horrible.

    At the time, I converted all my code to use complex<double> because that is essentially what the original complex had been, and have been using complex<double> since. I have a code that will need to work with very large data sets, so I need to use complex<float> instead. And now I get compile time errors for lines like 3 and 4. Is some way to allow a literal constant times a complex<float> to compile (without littering my code with a bunch of casts, like in line 5)?

    I'm using MS Visual Studio 2008 Standard Edition, if there's some compiler-specific flag that will fix this.

    Code:
    #include <complex>
    
    using namespace std;
    
    int dumdum(const complex<float> &cf, const complex<double> &cd)
    {
      complex<double> cd0;
      complex<float> cf0;
    
      cd0 = 2 * cd;    //  1. Error.  Easy work-around: use 2.0
      cd0 = 2.0 * cd;  //  2. No error.
      cf0 = 3.0 * cf;  //  3. An error?  Are you kidding me????
      cf0 = cf * 3.0;  //  4. Still an error.
      cf0 = cf * (float)3.0;  //  5. Kludge.  And ugly.
    
      return 0;
    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: compiler errors using complex<float> in C++

    Code:
      cf0 = cf * 3.0f;

  3. #3
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: compiler errors using complex<float> in C++

    Code:
    cf0 = 3.0f * cf;   
    cf0 = cf * 3.0f;
    3.0 is interpreted as a double, until you add the f-postfix.
    Last edited by laitinen; December 4th, 2008 at 03:08 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured