错误C2062:输入int意外

时间:2012-11-14 18:25:45

标签: c++ python swig

我是c ++和SWIG的新手

我正在windows环境中使用SWIG创建一个python模块。

创建包装类(example_wrap.cxx)之后。开始使用(python setup.py build_ext --inplace)构建python模块。

但我得到 * 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());
  }
}

请建议a纠正此错误

2 个答案:

答案 0 :(得分:1)

你不能在c ++中有一个没有返回类型的函数。您应该为函数GradedComplex设置返回类型。构造函数不能这样声明。

答案 1 :(得分:1)

您显然在某个头文件(GradedComplex中)某处声明了类GradedComplex.h?)

稍后您尝试在此行中使用此名称

GradedComplex(int n, double *thre);

对于人类读者来说,这一行可能看起来像是在尝试声明一个独立的函数GradedComplex。从技术上讲,拥有一个与现有类同名的函数是合法的。但是,由于您没有为此函数指定任何返回类型,因此编译器不会将此视为函数声明。编译器认为您正在尝试声明类型为GradedComplex的对象,并在声明符周围添加冗余括号,如

GradedComplex (a);

出于这个原因,int的外观会使其混淆并导致有关3090行中意外int的错误报告。

你想做什么?如果您尝试为GradedComplex定义构造函数,那么您已经知道如何执行它(您自己发布了正确的定义)。 3090线的目的是什么?你为什么写那条线?