以下のテストプログラムinput
の関数に示すように、C++を使用してベクトルオブジェクトのインスタンスを作成したいと思います。main()
ベクトルはデータで埋められ、関数input
への参照によって渡されcompute()
ます。計算関数は、2つのベクトルオブジェクトreal
とを返すことimag
です。
以下のコードスニペットに示されている方法で、これら2つのベクトルオブジェクトを返すことは可能ですか?
を使用してこのコードをコンパイルするgcc
と、次のエラーが発生します。
In function ‘void compute(const std::vector<double>&, std::vector<double>&, std::vector<double>&)’:
error: declaration of ‘std::vector<double> real’ shadows a parameter
error: declaration of ‘std::vector<double> imag’ shadows a parameter
おそらくこれを行うためのより良い方法がありますか?完全なテストプログラムは次のとおりです。
#include <iostream>
#include <vector>
void compute(const std::vector<double> &input,
std::vector<double> &real, std::vector<double> &imag)
{
unsigned int N = input.size();
unsigned int csize = (N / 2) + 1;
// the error occurs here
std::vector<double> real(csize);
std::vector<double> imag(csize);
for (int i = 0; i < csize; i++)
{
real[i] = input[i] * i;
imag[i] = input[i] * -i;
}
} // end
int main()
{
const int num = 10;
std::vector<double> input(num);
for(int i = 0; i < num; i++)
input[i] = i;
std::vector<double> real;
std::vector<double> imag;
compute(input, real, imag);
} // end