错误C2062:意外的int类型

2024-06-01 13:24:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我是c++和SWIG的新手

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

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

但是我得到了*example_wrap.cxx(3090):error c262:type'int'unexpected*

等级复杂.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(整数n,双*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更正此错误


Tags: includetypeitempushswigintgradestd
2条回答

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

显然,您在某个头文件(GradedComplex.h)中的某个地方声明了类GradedComplex

后来你试图在这行中使用这个名字

GradedComplex(int n, double *thre);

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

GradedComplex (a);

由于这个原因,这个int的出现混淆了它,并导致关于第3090行中意外的int的错误报告。

你想干什么?如果您试图为GradedComplex定义构造函数,那么您已经知道如何做了(您自己发布了正确的定义)。3090号线的用途是什么?你为什么写那句话?

相关问题 更多 >