3

I'm having trouble understanding on how would I call the add function in main if I want to add an instance to list.

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

my Main

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;

}

my Function in cpp

void list::Add(const Node myObject) {

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


head = temp;
size++; 

}

Im having trouble on this line myList.Add(myObject); keeps saying void list::Add(const list&)':cannot convert argument 1 from 'object' to 'const list::Node'

also no instance of overloaded function "list::Add" matches the argument list

2 Answers 2

1

You are trying to pass an object of type object into a function that takes a parameter of type Node. const Node myObject should be const object myObject.

Sign up to request clarification or add additional context in comments.

1 Comment

That explains alot. Thanks!!
1

This might give you some ideas:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.