Я пытаюсь включить STL std::valarray
в свою программу. Я хочу иметь возможность изменять части моей программы с чего-то вроде этого
for (unsigned int d = 0; d < DIM; ++d) {
position[d] += dt*(velocity[d] + a*force_new[d]);
force_old[d] = force_new[d];
}
на это
position += dt*(velocity + a*force_new);
force_old = force_new;
и
for (const auto& particle: particles) {
for (unsigned int d = 0; d < DIM; ++d) {
std::cout << particle.position[d] << " ";
}
std::cout << std::endl;
}
на что-то вроде
for (const auto& particle: particles) {
for(const auto& item: particle.position) std::cout << item << " ";
std::cout << std::endl;
}
В настоящее время структура данных, которую я использую, выглядит следующим образом:
struct Particle{
std::array<double, DIM> position;
std::array<double, DIM> velocity;
std::array<double, DIM> force_new;
std::array<double, DIM> force_old;
void update_position();
};
Я изменил эту часть моего кода на
struct Particle{
std::array<std::valarray<double>, DIM> position;
std::array<std::valarray<double>, DIM> velocity;
std::array<std::valarray<double>, DIM> force_new;
std::array<std::valarray<double>, DIM> force_old;
void update_position();
};
Однако, мой код делаетне компилируется, если я изменю свой код, как показано выше. Компилятор выдает ошибки типа
nbody.cpp: In member function ‘void Particle::update_position(double)’:
nbody.cpp:83:33: error: no match for ‘operator*’ (operand types are
‘const double’ and ‘std::array<std::valarray<double>, 2>’)
position += dt*(velocity + a*force_new);
и
nbody.cpp: In member function ‘void Nbody::print_data() const’:
nbody.cpp:45:60: error: no match for ‘operator<<’ (operand types are
‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const
std::valarray<double>’) for(const auto& item: particle.position)
std::cout << item << " ";