Extracting real part from complex number. Part II

I just had a thread closed on the subject (FFT):

http://www.cplusplus.com/forum/beginner/159038/

That code works well. In fact I need to make it dynamic, the array size will change during the program run many times. So, I declared input and output arrays as vectors and now again I am facing this incredible problem: to get a real part out of the array/vector. This a partial code (can post more if requested);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <vector>  
  int main ()
{
  std::vector<complex> Input, Output;
  complex valueC;
  int N=1024;
//.................
  for (int jj = 0; jj< N-1; jj++)
    {
      xx = blackmanHarrisWindow(jj,N);
      valueC = complex(xx,0);
      Input.insert (Input.begin() + jj, valueC);
      outFileInput << valueC.re() << std::endl;
//      outFileInput << (Input.begin() + jj).re() << std::endl;
    }


That last statement I commented out, I would like it to work but it does not compile. Again, I have no idea how to make this simple thing happen. I also checked <vector> header file and could not see anything remotely resembling complex numbers. For C++ all vectors seem to be real which is gross.

The whole code actually compiles (with that statement commented out) but it fails at the run. It says "segmentation fault, core dumped."

Where can I see core dumped? I have g++ in Ubuntu.

Thanks, - Alex
Last edited on
Hi Alex,

Clang++ adds a hint to the error message:
1
2
3
4
main.cpp:35: error: no member named 're' in '__gnu_cxx::__normal_iterator<complex *,
 std::vector<complex, std::allocator<complex> > >'; did you mean to use '->' instead of '.'?
      outFileInput << (Input.begin() + jj).re() << std::endl;
                                          ^

Two questions:
Why are you putting the same number in your output twice?
Is there a reason that you are using insert() instead of push_back()?
Hi norm b, I am not putting the same number in the output file twice. The second (commented out statement) did not work. When I tried it I commented out the previous statement. I haven't tried push_back(). Insert worked for me it seems.

"did you mean to use '->' instead of '.'?"

What do you mean by that? I don't quite understand. I may try the '->' later tonight, cannot do it now.

Thank you, - Alex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cassert>
#include <vector>

#include "complex" // non-standard

int main ()
{
  std::vector<complex> Input;
  complex valueC;
  int N=1024;
  Input.reserve(N-1); // You will need this much, so make it known.
//.................
  for (int jj = 0; jj < N-1; ++jj )
    {
      xx = blackmanHarrisWindow( jj, N );
      Input.emplace_back( xx, 0 ); // This is more efficient than push_back or insert
      assert( jj+1 == Input.size() );
      outFileInput << Input[ jj ].re() << '\n';
    }

Where was the beef?
1
2
3
4
5
6
7
8
9
10
11
Input.begin() // this is an iterator
(Input.begin() + jj) // this is an iterator too

*(Input.begin() + jj) // this is the jj'th element
Input[jj] // this is the jj'th element

Input[jj].re() // this calls member re() of jj'th element
(*(Input.begin() + jj)).re() // this calls member re() of jj'th element
(Input.begin() + jj)->re() // this calls member re() of jj'th element

(Input.begin() + jj).re() // this tries to call member re() of the iterator 


Last edited on
Topic archived. No new replies allowed.