-6

I am reading numbers from a big text file. So I have made a function that does that. It stores the numbers in the following array

**array = (double **)malloc(length*sizeof(double*));

I would like to store the values from this array in a std::complex<double> array or 2D array like this: a[i].real()=lines[i] but I can't find a way to do this.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • 4
    Why not read the data directly into a `std::vector>`? Also, you really should not use `malloc` in C++. – NathanOliver May 22 '18 at 19:06
  • it is C not C++ , if I read the data directly I get memory allocation segmentation fault bacause the array would be very big . – Horia Puscasu May 22 '18 at 19:10
  • 1
    @HoriaPuscasu *"it is C not C++"* There is no `std::complex` in c, and the tag says c++. – François Andrieux May 22 '18 at 19:11
  • 1
    What is C? Your question is tagged as C++ and `std::complex` is C++ so where is the C coming from? – NathanOliver May 22 '18 at 19:11
  • 1
    Are you talking an array of `double`, e.g. `double array[];` or are you talking a 2D (matrix) of `double`? Are you talking about converting a `double` to `std::complex`? I'm confused. – Thomas Matthews May 22 '18 at 19:31
  • If this is C then why do you have this tagged as C++? – Sailanarmo May 22 '18 at 19:44
  • @Sailanarmo The asker has tagged the question as C a couple times. However the data structure the asker is using is clearly C++ and not C, so we've been removing the C tag. – user4581301 May 22 '18 at 19:48
  • @user4581301 well OP should try and figure out what language he is programming in since he told us "It is C not C++". – Sailanarmo May 22 '18 at 19:50
  • @Sailanarmo the language confusion happens more often than I'd like, probably because C is frequently taught as C++, so it's better to just correct the asker, update the tags, and carry on. – user4581301 May 22 '18 at 19:52
  • @user4581301 the very idea of that makes me sick and I want to barf in fortran-77... – Swift - Friday Pie May 22 '18 at 20:09

2 Answers2

2

I'm going to take a wild stab at this and assume you want to read a file of double precision numbers into a database of std::complex<double>.

Here's an example:

double real_part = 0.0;
double imaginary_part = 0.0;
std::vector<std::complex<double> > database;
while (my_data_file >> real_part >> imaginary_part)
{
  std::complex<double> combined;
  combined.real(real_part);
  combined.imag(imaginary_part);
  database.push_back(combined);
}

There are other methods to read in the data, but the above illustrates the fundamental concepts.

In the above code snippet, the real and imaginary components are read in from the data file.

A complex number is defined. The real and imaginary components are set into the combined complex number.

Finally, the complex number is appended to the database.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
1

It looks like Thomas Matthews has provided how to do what it looks like you're trying to do, so I'll just explain why what you tried doesn't work.

The data types are the the same, double, but the value categories are not. std::complex::real() returns an un-assignable prvalue, not an assignable lvalue.

The value returned by a[i].real() only exists for the blink of an eye, the end of the line, so assigning to it is completely useless. The C++ standard requires the compiler to reject the assignment to prevent the errors that would result.

You could use the std::complex::real(double) overload

a[i].real(lines[i]);

But use the other answer. It will result in much less grief.

Further reading: What are rvalues, lvalues, xvalues, glvalues, and prvalues?

user4581301
  • 33,082
  • 7
  • 33
  • 54