C++错误,显示创建链接列表调用"error LinkedList Interface is an inaccessable base of linkedlist"



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

在文件工厂中.cpp

#include "list.h"
using namespace std;
LinkedListInterface * Factory::getLinkedList
{
      return new linkedlist();
}

在文件中 工厂.h

#pragma once
#include "LinkedListInterface.h"
using namespace std;
class Factory
{
     public:
         static LinkedListInterface * getLinked();
};

file list.h,我有一个基本的构造函数,该类称为linkedlist #include 使用命名空间标准;

 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 一次 #include

using namespace std;
class LinkedListInterface
{
public:
    LinkedListInterface(void){};
    virtual ~LinkedListInterface(void){};

问题

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

运算符 new 在调用 linkedlist() 构造函数后返回 linkedlist*。在代码中,假设转换为 LinkedListInterface * 不正确,对于编译,必须显式转换。

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

相关内容

最新更新