C ++不确定如何将一个类用作另一个类中的变量 [英] C++ unsure how to use one class as a variable in another class

查看:84
本文介绍了C ++不确定如何将一个类用作另一个类中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

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



Hi there!
So, my assignment is to make a book inventory from a file that gets read by the program and put into a linked list. All of the classes, members, constructors and functions seen below are required to be in the program. The only added constructed I add is

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





我尝试了什么:



我刚才收到的错误是警告:未使用的变量'已发布'。



我不认为我在



What I have tried:

The error I'm getting just says "warning: unused variable 'published'.

I don't think I'm using

Date *published
  correctly in 

class =code-keyword> class Book

class Book

。我试图在

class Date

那里的变量,但这似乎只会让我的问题变得更糟。



我会感激任何推动正确的方向。谢谢!以节省空间的名义,我遗漏了一些我认为有效的东西ne,但我可以在这里添加它,如果它会有所帮助。



out of the variables in there, but that seemed to only make my problems worse.

I would appreciate any nudge in the right direction. Thanks! In the name of saving space, I've left out some stuff that I think is working fine, but I can add back in here if it would be helpful.

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



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 >> publisher;
        myfile >> price;
        myfile >> isbn;
        myfile >> pages;
        myfile >> copies;
    }

    myList.print_list();

    return 0;
}

推荐答案

不要将指针用于简单类日期。当您的类具有大数据(复制大数据会降低性能)或者具有一些无法轻松复制的元素(例如用于数据库连接,文件或网络操作的类等)时,需要指针。因此,在您的情况下,您可以按原样使用日期:

Do not use pointer for your simple class Date. A pointer is required when your class either has big data (copying of big data reduces performance) or has some elements that cannot be easily copied (such as a class for a database connection, file or network operations, etc). So, in your case you can use the Date as is:
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);
};


Quote:

Book :: Book (无效){

title =;

author =;

Date * published = NULL; //< ------------ BUG HERE

publisher =;

price = 0;

isbn =;

pages = 0;

copies = 0;

}

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



虽然这个bug在上面的代码中你应该写的代替


While The bug is in the above code where you should write instead

published = NULL;



或更好


or, better

published = nullptr;





我的第二个 Andrew Cherednik 关于他的建议:不要使用指针 Date member。



I second Andrew Cherednik on his suggestion: don't use a pointer for the Date member.


这篇关于C ++不确定如何将一个类用作另一个类中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆