如果我想在列表中添加实例,我无法理解如何在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); 继续说空列表::添加(常量列表&amp;)':不能将参数1从'对象'转换为'常量列表::节点'
也没有重载函数的实例“list :: Add”匹配参数列表
答案 0 :(得分:1)
您正在尝试将类型为object的对象传递给采用Node类型参数的函数。 const Node myObject应该是const对象myObject。
答案 1 :(得分:1)
这可能会给你一些想法:
#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);
}