-1

我写了一个类来帮助学习数据结构。我以前成功使用过这种方法,但这次不喜欢return new linkedlist();

在文件Factory.cpp中

#include "list.h"
using namespace std;

LinkedListInterface * Factory::getLinkedList
{
      return new linkedlist();
}

在文件Factory.h

#pragma once
#include "LinkedListInterface.h"
using namespace std;

class Factory
{
     public:
         static LinkedListInterface * getLinked();

};

文件list.h,我在其中有一个基本构造函数,该类称为linkedlist #include using namespace std;

 class linkedlist
 {
 private:
     typedef struct node
     {
         int data;
         node* next;
     }* nodePtr;
     nodePtr head;
     nodePtr curr;
     nodePtr temp;

  public:
         linkedlist()
         {
             head = NULL;
             curr = NULL;
             temp = NULL;
         }
   ......
   };
  there are other functions but i dont think they causing my problem.

这是来自我教授的 LinkeListInterface.h。文件的其余部分是虚拟方法,我确保将它们包含在 list.h #pragma once #include

using namespace std;

class LinkedListInterface
{

public:

    LinkedListInterface(void){};
    virtual ~LinkedListInterface(void){};
4

1 回答 1

0

问题在

LinkedListInterface * Factory::getLinkedList
{
      return new linkedlist();
}

operator new 在调用linkedlist() 构造函数后返回linkedlist*。这里的代码假设转换为 LinkedListInterface * 不正确,为了编译,必须显式转换。

LinkedListInterface * Factory::getLinkedList()
{
      return (LinkedListInterface* ) new linkedlist();
}
于 2013-09-30T02:45:46.130 回答