2016-04-07 29 views
3

Listeye örnek eklemek istiyorsanız, ekleme işlevini ana hat olarak nasıl arayacağımı anlayamıyorum. cpp'de içindeBir Düğüm kullanarak bir Bağlantılı Listeye Örnek Ekleme

#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); 

benim Esas

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; 

} 

benim Fonksiyon

void list::Add(const Node myObject) { 

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


head = temp; 
size++; 

} 

Im bu hat myList.Add (myObject) üzerine sorun yaşıyorsanız; ':: ekle listesi' Uç '

da

aşırı fonksiyonunun hiçbir örneği :: const listesi 'için' nesne' dan bağımsız değişkeni 1 dönüştüremiyor' argüman maçları geçersiz liste :: (const liste &) ekleyin söyleyip duruyor list

cevap

1

Yazım nesnesinin bir nesnesini, Düğüm türünde bir parametre alan bir işleve geçirmeye çalışıyorsunuz. const Düğüm myObject const nesne myObject olmalıdır.

+0

sürü açıklıyor. Teşekkürler!! – Carlitos

1

Bu size bazı fikirler verebilir:

#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); 
} 
İlgili konular