c++ - 与在 g++ 中编译 C++ 应用程序有关的问题(可能原因 #ifndef)

标签 c++ g++ compilation

我正在尝试使用 C++ 编程语言和继承等功能构建链表应用程序。

我已将接口(interface)和实现拆分到不同的文件中,但无法编译。

下面是文件列表

接口(interface)文件:- node.h、abstractList.h、singleLinkedList.h

实现文件:singleLinkedList.cpp

节点.h

#ifndef NODE_H
#define NODE_H

#include <iostream>

struct nodeType {
        int data;
        struct nodeType *next;
}listNode;


#endif

抽象列表.h

#ifndef ABSTRACT_LIST_H
#define ABSTRACT_LIST_H

#include <iostream>
#include "node.h"
#include "singleLinkedList.h"

class abstractList {
        public:
        virtual ~abstractList();
        virtual bool isEmpty(Node* ) = 0;
        virtual int get(const int&) = 0;
        virtual int indexOf(const int& ) = 0;
        virtual Node insert(const int& , const int& ) = 0;
        virtual void delete(const int& ) = 0;
};

#endif

单链表.h

#ifndef SINGLE_LIST_H
#define SINGLE_LIST_H

#include <iostream>
#include "node.h"
#include "abstractList.h"

class singleLinkedList : public abstractList {

        public:

        singleLinkedList();
        ~singleLinkedList();
        Node populateList( );

        private:

        void checkIndex();
        int data;
        Node head;
};

#endif

到目前为止,我刚刚在实现文件中编写了 populateList() 函数,下面是实现文件。

单链表.cpp

#include <iostream>
#include "node.h"
#include "singleLinkedList.h"
#include "abstractList.h"


    Node singleLinkedList :: populateList()
    {
            Node temp;
            int data;
            temp = head;
            char ch;
            std::cout<<"Enter Data? (y/n) " << std::endl;
            std::cin>>ch;

            while(ch == 'Y' || ch == 'y')
            {
                    std::cout<<"Enter the data that you would like to store.\n"<<std::endl;
                    std::cin>>data;
                    temp = new Node();
                    temp->data = data;
                    temp->next = head;
                    head = temp;
                    std::cout<<"Enter more data?"<<std::endl;
                    std::cin>>"\n">>ch;
            }

            return temp;
    }

当我给出 g++ -c singleLinkedList.cpp 时,我遇到了很多错误。我很确定我做了一些愚蠢的事情。谁能指出我的错误?

编辑:特定问题的错误日志。

struct nodeType {
int data;
struct nodeType *next;
}listNode;

虚拟列表节点 *insert();

以上说法正确吗?

谢谢 凯利

最佳答案

delete 是 C++ 中的关键字,不能将其用作方法名。您需要在此处使用不同的名称:

class abstractList {
        public:
        //...
        virtual void delete(const int& ) = 0;
        //-----------^^^^^^ rename this.
};

关于c++ - 与在 g++ 中编译 C++ 应用程序有关的问题(可能原因 #ifndef),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7061899/

相关文章:

c++ - valgrind 不会提示内存损坏?

c++ - CMake 在链接之前指定源

g++ - "Illegal instruction: 4"出现在 OS X Lion 中

c - 什么是 C 中的目标文件?

java - 动态编译源文件时如何为JavaCompiler提供接口(interface)?

linux - fmem 编译错误与 make

c++ - 指向基类的指针在 while 循环中丢失,导致段错误。 C++

c++ - 如何加快C++中2D三角矩阵的内存分配?

c++ - 为什么销毁被 placement new 覆盖的对象不是未定义的行为?

c++ - 新的,Microsoft 除外