Cython使模板类有错误

时间:2016-06-09 06:42:59

标签: python python-3.x cython python-extensions

# distutils: language = c++
# distutils: sources = cppcode.cpp 
from libcpp.string cimport string

cdef extern from "cppcode.h" namespace "mynamespace":
cppclass LinkedList[T]:
    LinkedList ()
    void append (T)
    int getLength ()
    void remove (int)
    void printList ()
    int tes ()

cdef class linklist:

    cdef LinkedList[string] * listt

def __cinit__(self):
    self.listt = new LinkedList[string]()
    if self.listt == NULL:
        raise MemoryError()

def __dealloc__(self):
    if self.listt != NULL:
        del self.listt

cpdef int tests(self):
    return self.listt.tes()

cpdef void appendlist(self,value):
    return self.listt.append(value)

cpp代码:     #包括     #include" cppcode.h"

using namespace std;

template <class T>
void mynamespace::LinkedList<T>::append (T value)
{
   if (head == NULL)
     {
       head = new ListNode (value);
       tail = head;
     }
   else
     {
       tail->next = new ListNode (value);
       tail = tail->next;
     }
   length++;
}

template <class T>
void mynamespace::LinkedList<T>::printList (void)
{
  if (head != NULL)
    {
      ListNode * it = head;
      while (it != NULL)
    {
      cout << it->value << endl;
      it = it->next;
    }
    }
}

template <class T>
int mynamespace::LinkedList<T>::getLength (void)
{
  return length;
}

template <class T>
int mynamespace::LinkedList<T>::tes ()
{
  return 10;
}

template <class T>
void mynamespace::LinkedList<T>::remove (int idx)
{
  if (head != NULL)
    {
      // invalid element to remove...
      if (idx < 0 || idx > length)
    return;

      if (idx == 0)
    {
      ListNode * tmp = head;
      head = tmp->next;
      delete tmp;
      length--;
    }
      else
    {
      ListNode * it = head;
      ListNode * parent = it;

      int i;
      bool found = false;

      for (i = 0; i < length; ++i)
      {
          if (i == idx)
        {
          found = true;
          break;
        }
          parent = it;
          it = parent->next;
      }

      if (found)
      {
          parent->next = it->next;
          delete it;
          length--;
      }
    }
     }
}

我尝试按cython创建一个类,但是当我将它编译为.pyd文件时会发生错误。但是当我删除函数(test)和(appendlist)时,它可以通过编译。

错误信息如下:

cpycode.obj : warning LNK4197: export 'PyInit_cpycode' specified multiple times; using first specification
Creating library build\temp.win-amd64-3.5\Release\cpycode.cp35-win_amd64.lib and object build\temp.win-amd64-3.5\Release\cpycode.cp35-win_amd64.exp
cpycode.obj : error LNK2001: unresolved external symbol "public: int __cdecl mynamespace::LinkedList<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::tes(void)" (?tes@?$LinkedList@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@mynamespace@@QEAAHXZ)
C:\pptemplates\cpycode.cp35-win_amd64.pyd : fatal error LNK1120: 1 unresolved externals
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\amd64\\link.exe' failed with exit status 1120

任何人都可以提供帮助吗?感谢

0 个答案:

没有答案