【问题标题】:Adding an Instance to a Linked List using a Node使用节点将实例添加到链表
【发布时间】:2016-07-27 18:12:40
【问题描述】:

如果我想将实例添加到列表中,我无法理解如何在 main 中调用 add 函数。

#include "object.h"
class list {

private:
struct Node
{
object objectInfo;
Node *next;

};


int size;
Node *head;


public:

list();
list(const list& otherlist);
~list();

void Add(const Node myObject);

我的主要

int main() {
object myObject;

myObject.setTitle("Object 1");
myObject.setPrice(78.58);
myObject.setISBN("515161611");


cout << myObject << endl;

list myList;

myList.Add(myObject);


return 0;

}

我在 cpp 中的函数

void list::Add(const Node myObject) {

Node* temp;
temp = new Node; 
temp->objectInfo = myObject.objectInfo;
temp->next = head; 


head = temp;
size++; 

}

我在这条线上遇到了麻烦 myList.Add(myObject); 一直说 void list::Add(const list&)':cannot convert argument 1 from 'object' 到 'const list::Node'

也没有重载函数“list::Add”的实例与参数列表匹配

【问题讨论】:

    标签: c++ linked-list add nodes


    【解决方案1】:

    您正试图将 object 类型的对象传递给采用 Node 类型参数的函数。 const Node myObject 应该是 const object myObject。

    【讨论】:

    • 这解释了很多。谢谢!!
    【解决方案2】:

    这可能会给你一些想法:

    #include <iostream>
    #include <string>
    
    template <typename Object>
    class List {
        class Node : public Object {
            //static int& count() { static int c=0; return c; }
        public:
            Node* next;
            Node() : next(nullptr) { 
                //std::cout << ++count() << " Nodes\n"; 
            }
            Node(const Object& obj) : next(nullptr), Object(obj) { 
                //std::cout << ++count() << " Nodes\n";
            }
            ~Node() {
                //std::cout << --count() << " Nodes\n";
            }
        };
    
        Node *head, *tail;
        int size;
    
    public:
        class iterator {
            Node *cur_node;
        public:
            iterator(Node* node) { cur_node = node; }
            Object& operator * () const { return *cur_node; }
            bool operator != (const iterator& iter) const { return iter.cur_node != cur_node; }
            iterator& operator ++() { if(cur_node) cur_node = cur_node->next; return *this;  }
        };
        class iterator_const {
            const Node *cur_node;
        public:
            iterator_const(const Node* node) { cur_node = node; }
            const Object& operator * () const { return *cur_node; }
            bool operator != (const iterator_const& iter) const { return iter.cur_node != cur_node; }
            iterator_const& operator ++() { if(cur_node) cur_node = cur_node->next; return *this; }
        };
    
        iterator       begin()       { return iterator(head); }
        iterator       end()         { return iterator(nullptr); }
        iterator_const begin() const { return iterator_const(head); }
        iterator_const end()   const { return iterator_const(nullptr); }
    
        template <typename ...Args>
        void Add(const Object& obj, Args...more) {
            Node *new_node = new Node(obj);
            ++size;
            if(!tail) { tail = head = new_node; }
            else      { tail->next = new_node; tail = new_node; }
            Add(more...);
        }
        void Add() {}
    
        int Size() const { return size; }
    
        List() : head(nullptr), tail(nullptr), size(0) {}
        List(const List& src) : head(nullptr), tail(nullptr), size(0) {
            for(auto&& entry : src) {
                Add(entry);
            }
        }
        ~List() {
            Node* p = head;
            while(p) {
                Node* next = p->next;
                delete p;
                p = next;
            }
        }
    };
    
    struct MyObjectType {
        std::string name;
        int         age;
        MyObjectType(std::string name, int age) : name(name), age(age) {}
        friend std::ostream& operator << (std::ostream& os, const MyObjectType& obj) {
            return os << "{\"" << obj.name << "\":" << obj.age << "}";
        }
    };
    
    template <typename T>
    void PrintList(const List<T>& list) {
        std::cout << "Size: " << list.Size() << "\n";
        for(const auto &elem : list) {
            std::cout << "    " << elem << "\n";
        }
    }
    
    int main() {
        using MyList = List<MyObjectType>;
    
        MyList list_one;
        list_one.Add(
            MyObjectType("Harry",32),
            MyObjectType("Lisa", 66),
            MyObjectType("Buddy", 2),
            MyObjectType("Skippy", 21)
        );
    
        MyList list_two(list_one);
        list_two.Add(
            MyObjectType("Horse", 10),
            MyObjectType("Mule", 11)
        );
    
        std::cout << "list_one:\n";
        PrintList(list_one);
        std::cout << '\n';
        std::cout << "list_two:\n";
        PrintList(list_two);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      相关资源
      最近更新 更多