0

私が書いているプログラムの一部として、double のベクトルと複素 double のベクトルの外積を求める必要があります。これを行う必要があると思われる関数を作成しましたが、呼び出すと次のエラーが発生します。

error: no matching function for call to ‘CrossProduct1D(std::vector< double, std::allocator<double> >&, std::vector<std::complex<double>, std::allocator<std::complex<double> > >&)’

これが私のコードです:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <math.h>
#include <complex>
using namespace std;

//1D cross product
template <typename T>
vector<T> CrossProduct1D(vector<T> const &a, vector<T> const &b)
{
  vector<T> r (a.size());  
  r[0] = a[1]*b[2]-a[2]*b[1];
  r[1] = a[2]*b[0]-a[0]*b[2];
  r[2] = a[0]*b[1]-a[1]*b[0];
  return r;
}

//::::::::importing data from text::::::::::
  vector<string> ezivec;
  ezivec.reserve(4000);

  string ezidat("ez.i.txt");

  ifstream ezifile;
  ezifile.open(ezidat.c_str());

  if(!ezifile.is_open())
    {
      cerr<<"Error opening file : "<<ezidat.c_str()<<endl;
      return -1;
    }

  string ezistr; //store lines in vector
  while(getline(ezifile, ezistr, ';'))
    {
      ezivec.push_back(ezistr);
    }

  ezifile.close();

 //Converting from vector of strings to vector of floats
  vector<double> ezi (ezivec.size());
  for(int i = 0; i < ezivec.size(); ++i)
    {
      ezi[i] = string_to_T<double>(ezivec[i]);
    }

 //

  vector<string> ezrvec;
  ezrvec.reserve(4000); 

  string ezrdat("ez.r.txt");

  ifstream ezrfile;
  ezrfile.open(ezrdat.c_str());

  if(!ezrfile.is_open())
    {
      cerr<<"Error opening file : "<<ezrdat.c_str()<<endl;
      return -1;
    }

  string ezrstr;
  while(getline(ezrfile, ezrstr, ';'))
    {
      ezrvec.push_back(ezrstr);
    }

  ezrfile.close();

  vector<double> ezr (ezrvec.size());
  for(int i = 0; i < ezrvec.size(); ++i)
    {
      ezr[i] = string_to_T<double>(ezrvec[i]);
    }

//:::::::defining vectors:::::::
vector<vector<complex<double> > > E0 (ezi.size(), vector<complex<double> > (3));
for(int i = 0; i < ezi.size(); i++)
{
  E0[i][0].real() = 0.0;
  E0[i][0].imag() = 0.0;
  E0[i][1].real() = 0.0;
  E0[i][1].imag() = 0.0;;
  E0[i][2].real() = ezr[i];
  E0[i][2].imag() = ezi[i];
} 

vector<double> n_a (3);
n_a[0] = 1.0;
n_a[1] = 0.0;
n_a[2] = 0.0;

//:::::::calling cross product:::::::
for(int j = 1; j < jmax; j++)
    {
      M[j] = CrossProduct1D(n_a, E0[j]);
    }

「ez.i.txt」と「ez.r.txt」は、セミコロン区切りの 4000 個のテキスト ファイルです。

4

1 に答える 1

10

テンプレート関数は単一のタイプ、、でパラメーター化されており、T2vector<T>つかかりますが、2つの異なるタイプのベクトルを渡そうとしているため、T選択できる単一の関数はありません。

2つのテンプレートパラメータを持つことができます。

template<class T, class U> CrossProduct1D(std::vector<T> const& a,
                                          std::vector<U> const& b)
于 2012-07-19T17:28:59.973 に答える