1

私はc ++とSWIGが初めてです

Windows環境でSWIGを使用してpythonモジュールを作成しています。

ラッパー クラス (example_wrap.cxx) を作成した後。Pythonモジュールの作成に(python setup.py build_ext --inplace)を使用してビルドを開始しました。

しかし、私は*example_wrap.cxx(3090) : エラー C2062: タイプ 'int' 予期しない*を取得しています

GradedComplex.h:

class GradedComplex
{
public:
  typedef std::complex<double> dcomplex;
  typedef Item<dcomplex> item_type;
  typedef ItemComparator<dcomplex> comparator;
  typedef std::set<item_type, comparator> grade_type;

private:
  int n_;
  std::vector<grade_type *> grade_;
  std::vector<double> thre_;

public:
  GradedComplex(int n, double *thre);
  ~GradedComplex();

  void push(item_type item);
  void avg(double *buf);
};

#endif

GradedComplex.cc

GradedComplex::GradedComplex(int n, double *thre)
{
  n_ = n;
  for (int i = 0; i < n_; ++i)
  {
    thre_.push_back(thre[i]);
    grade_.push_back(new grade_type());
  }
}

次に、SWIG を使用して Python モジュールを生成するためにビルドします。

Swig インターフェイス ファイル (example.i) GradedComplex(int n, double *thre);

私はSWIGインターフェースファイルの専門家ではありません

生成されたラッパー クラスには大量のコードがあるため、いくつか貼り付けています。

コード: example_wrap.cxx

3083: #define SWIG_FILE_WITH_INIT
3084: #include "Item.h"
3085: #include "GradedComplex.h"
3086: typedef std::complex<double> dcomplex;
3087: typedef Item<dcomplex> item_type;
3088: typedef ItemComparator<dcomplex> comparator;
3089: typedef std::set<item_type, comparator> grade_type;   
3090: GradedComplex(int n, double *thre);
3091: void push(item_type item);
3092: void avg(double *buf);
3093: #include <string>
3094: #include <complex> 
3095: #include <iostream>
3096: #if PY_VERSION_HEX >= 0x03020000
3097: # define SWIGPY_SLICE_ARG(obj) ((PyObject*) (obj))
3098: #else
3099: # define SWIGPY_SLICE_ARG(obj) ((PySliceObject*) (obj))
3100: #endif

GradedComplex コンストラクター:

GradedComplex::GradedComplex(int n, double *thre)
{
  n_ = n;
  for (int i = 0; i < n_; ++i)
  {
    thre_.push_back(thre[i]);
    grade_.push_back(new grade_type());
  }
}

このエラーを修正することを提案してください

4

2 に答える 2

1

C++ では、戻り値の型を持たない関数を使用することはできません。関数の戻り値の型を設定する必要がありますGradedComplex。コンストラクターはそのように宣言することはできません。

于 2012-11-14T18:37:42.803 に答える
1

GradedComplexヘッダー ファイルのどこかでclass を宣言したようです ( GradedComplex.h?)

後でこの行でこの名前を使用しようとしました

GradedComplex(int n, double *thre);

人間の読者にとって、この行は独立した関数を宣言しようとしているように見えるでしょうGradedComplex。技術的には、既存のクラスと同じ名前の関数を持つことは合法です。ただし、この関数には戻り値の型が指定されていないため、コンパイラはこれを関数宣言として認識しません。GradedComplexコンパイラは、次のように、宣言子の周りに冗長な括弧を使用して型のオブジェクトを宣言しようとしていると見なします。

GradedComplex (a);

このため、その外観はそれを混乱させ、3090 行intの予期しないエラー レポートにつながります。int

何をしようとしていたのですか?のコンストラクターを定義しようとしていた場合はGradedComplex、その方法を既に知っています (自分で正しい定義を投稿しました)。行 3090 の目的は何ですか? なぜその行を書いたのですか?

于 2012-11-14T18:40:32.173 に答える