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

发布于 2024-11-29 11:02:20 字数 2524 浏览 1 评论 0原文

我正在尝试使用 C++ 编程语言构建一个链接列表应用程序。 例如继承等。

我已经拆分了界面和功能, 在不同的文件中实现但无法编译。

列表

接口文件:-node.h,abstractList.h,singleLinkedList.h

文件:singleLinkedList.cppnode.habstractList.hsingleLinkedList.h

到目前为止

#ifndef NODE_H
#define NODE_H

#include <iostream>

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


#endif

实现

#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

以下是文件

#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()函数文件,这里是实现文件。

singleLinkedList.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();

上述说法正确吗?

谢谢 凯莉

I am trying to build a Linked list application using C++ programming language & features such as inheritance etc.

I have split the interface & implementation in different files but not able to compile.

Below are the list of files

Interface files :- node.h , abstractList.h , singleLinkedList.h

Implementation files: singleLinkedList.cpp

node.h

#ifndef NODE_H
#define NODE_H

#include <iostream>

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


#endif

abstractList.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

singleLinkedList.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

So far i have just coded the populateList() function in the implentation file, here goes the implementation file.

singleLinkedList.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;
    }

When i give g++ -c singleLinkedList.cpp , i am getting lot of errors. I am pretty sure i have done something stupid. Can anyone please pin point my error?

EDIT: Error Log With specfic issues.

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

virtual listNode *insert();

Is the above statement correct?

Thanks
Kelly

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没有 2024-12-06 11:02:20

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

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

delete is a keyword in C++, you can't use it as a method name. You need to use a different name here:

class abstractList {
        public:
        //...
        virtual void delete(const int& ) = 0;
        //-----------^^^^^^ rename this.
};
半世晨晓 2024-12-06 11:02:20

问题出在你的 typedef 中:

typedef listNode *Node;

意味着 Node 的所有实例基本上都会被 listnode* 替换,

  temp = new Node();

实际上读取的

  temp = new listnode*();

是 But new Foo() 会返回一个Foo*(因为 new 返回一个指向为对象分配的内存的指针),这意味着 new listnode*() 将返回一个 listnode**temp 作为一个 listnode* 不知道 listnode** 是什么并且抱怨。

你想要做的是:

 Node temp = new listnode();

或者完全忘记 typedef:

 listnode* temp = new listnode();

The problem is in your typedef:

typedef listNode *Node;

means that all instances of Node will essentially replaced by listnode*

  temp = new Node();

actually reads

  temp = new listnode*();

But new Foo() would return a Foo* (because new returns a pointer to memory allocated for an object), meaning that new listnode*() would return a listnode**. temp being a listnode* has no Idea what a listnode** is and complains.

what you want to do is:

 Node temp = new listnode();

or forget the typedef altogether:

 listnode* temp = new listnode();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文