C ++不确定如何将另一个类中的类用作变量

时间:2017-09-26 03:17:35

标签: c++

所以,我的任务是从一个文件制作一本书库,该文件被程序读取并放入链表中。下面看到的所有类,成员,构造函数和函数都必须在程序中。我添加的唯一添加的是

Date(unsigned int, unsigned int, unsigned int);

我得到的错误只是说“警告:未使用的变量'已发布'。

我认为我在Date *published中没有正确使用class Book。我试图在class Date中制作一个结构,但这似乎只会让我的问题变得更糟。

我很感激任何朝着正确方向的推动。谢谢!在节省空间的名义下,我遗漏了一些我认为工作正常的东西,但如果它有用的话,我可以在这里添加。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Date{
public:
    unsigned int day;
    unsigned int month;
    unsigned int year;
    Date(void);
    Date(unsigned int, unsigned int, unsigned int);
    ~Date(void);
};

Date::Date(void){
    day = 0;
    month = 0;
    year = 0;
}

Date::Date(unsigned int day, unsigned int month, unsigned int year){
    day = day;
    month = month;
    year = year;
}

Date::~Date(void){
}

class Book{
public:
    string title;
    string author;
    Date *published;
    string publisher;
    float price;
    string isbn;
    unsigned int pages;
    unsigned int copies;
    Book(void);
    Book(string, string, Date, string, float, string, unsigned int, unsigned int );
    ~Book(void);
};

Book::Book(void){
    title = "";
    author = "";
    Date *published = NULL;
    publisher = "";
    price = 0;
    isbn = "";
    pages = 0;
    copies = 0;
}

Book::Book( string title, string author, Date published, string publisher,
           float price, string isbn, unsigned int pages, unsigned int copies){
    title = title;
    author = author;
    published = published;
    publisher = publisher;
    price = price;
    isbn = isbn;
    pages = pages;
    copies = copies;
}

void LinkedList::print_list(void){
    Node *temp = head;
    while( temp != NULL ){
        cout << temp->book->title << endl;
        cout << temp->book->author << endl;
        cout << temp->book->published << endl;
        cout << temp->book->publisher << endl;
        cout << temp->book->price << endl;
        cout << temp->book->isbn << endl;
        cout << temp->book->pages << endl;
        cout << temp->book->copies << endl;
        temp = temp->next;
        cout << endl;
    }
}

LinkedList::~LinkedList(void){
}

int main()
{
    LinkedList myList;
    ifstream myfile("booklist.txt");

    string title;
    string author;
    Date published;
    string publisher;
    float price;
    string isbn;
    unsigned int pages;
    unsigned int copies;

    while( myfile ){
        myList.insert_front( new Book(title, author, published, publisher,
                                      price, isbn, pages, copies));

        myfile >> title;
        myfile >> author;
        myfile >> published;
        myfile >> publisher;
        myfile >> price;
        myfile >> isbn;
        myfile >> pages;
        myfile >> copies;
    }

    myList.print_list();

    return 0;
}

0 个答案:

没有答案