我不能打印我的书的名字,但可以打印其他所有的东西

I cant print the name of my book but can print everything else

本文关键字:打印 其他 但可以 不能 我的书      更新时间:2023-10-16

我的cpp文件,由于某种原因,我可以打印我认为是我的主文件的书名旁边的所有其他内容。当我构建它时,我没有遇到任何错误,但当我运行它时,小窗口会出现并消失

#include "BookRecord.h"
BookRecord::BookRecord()
{
    m_sName[128]=NULL;
    m_lStockNum=0;
    m_iClassification = 0;
    m_dCost = 0.0;
    m_iCount = 0;
}
BookRecord::BookRecord(char *name, long sn, int cl, double cost)
{
    m_iCount=1;
    m_sName[128]=*name;
    m_lStockNum=sn;
    m_iClassification=cl;
    m_dCost=cost;
}
BookRecord::~BookRecord()
{
}
void BookRecord::getName(char *name)
{
    strcpy(name, m_sName);
}
void BookRecord::setName(char *name)
{
    strcpy(m_sName, name);
}
long BookRecord::getStockNum()
 {
     return m_lStockNum;
 }
void BookRecord::setStockNum(long sn)
{
    m_lStockNum = sn;
}
void BookRecord::getClassification(int& cl)
{
    cl=m_iClassification;
}
void BookRecord::setClassification(int cl)
{
    m_iClassification= cl;
}
void BookRecord::getCost(double *c)
{
    *c = m_dCost;
}
void BookRecord::setCost(double c)
    { 
        m_dCost = c;
    }
int BookRecord::getNumberInStock()
 {
     return m_iCount;
 }
 void BookRecord::setNumberInStock(int count)
 {
     count = m_iCount;
 }
void BookRecord::printRecord()
 {
     //cout << "Name: " <<m_sName <<"n";
     printf("Name: %s", m_sName);
     cout<<endl;
     cout<<"Stock Number: "<<m_lStockNum<<"n";
     cout<<"Class: " <<m_iClassification<<"n";
     cout<<"Cost: $"<<m_dCost<<"n";
     cout<<"In Stock: "<<m_iCount<<"n";
 }

我的主文件

#include "BookRecord.h"
int main()
{
    char *bName="C Plus Plus";
    BookRecord *Ana = new BookRecord(bName, 1, 1, 50.9);
    /*char * bookName="";
    int clNo;
    double c;
    Ana->getClassification(clNo);
    Ana->getCost(&c);
    Ana->getName(bookName);*/
    cout << "Printing using printRecord() Function" << endl;
    Ana->printRecord();
    /*cout << endl << "Printing using getter properties" <<endl;
    cout << bookName << endl;
    cout<< clNo << endl;
    cout << c << endl;
    cout << Ana->getNumberInStock() << endl;*/
    return 0;
}

嗨,如果不使用char m_sName[128],我会做一些不同的事情,我宁愿使用std::string或指针,然后在destructor中销毁它。我在这里的代码基于您上面已经请求的内容,并且我正在使它尽可能简单。我知道这里可以做得更好,但你来了:

编辑:重新制作了一点,问题越来越多。

BookRecord.h

#ifndef __BOOKRECORD_H__
#define __BOOKRECORD_H__
#ifndef MAX_NAME_SIZE
#define MAX_NAME_SIZE 128
#endif
class BookRecord
{
private:
    char m_sName[MAX_NAME_SIZE] = {}; // I would have used a pointer or std:string and not char
    long m_lStockNum = 0;
    int m_iClassification = 0;
    double m_dCost = 0.0;
    int m_iCount = 0;
public:
    BookRecord();
    BookRecord(char* name, long sn, int cl, double cost);
    ~BookRecord();
    // EDIT: Your question grew
    char* GetName();
    void SetName(char *name);
    long GetStockNum();
    void SetStockNum(long sn);
    int GetClassification();
    void SetClassification(int cl);
    double GetCost();
    void SetCost(double c);
    int GetNumberInStock();
    void SetNumberInStock(int count);
    void PrintRecord();
};
#endif

BookRecord.cpp

#include <stdlib.h>
#include <iostream>
#include <stdexcept>
#include "BookRecord.h"
using namespace std;
BookRecord::BookRecord()
{
}
BookRecord::BookRecord(char* name, long sn, int cl, double cost) : 
    m_iCount(1), m_lStockNum(sn), m_iClassification(cl), m_dCost(cost)
{
    if (name == NULL)
        throw std::invalid_argument("Name of book is null");
    if (strlen(name)>MAX_NAME_SIZE)
        throw std::invalid_argument("Name of book is to long");
    memset(&m_sName, 0, sizeof(char)*MAX_NAME_SIZE);
    memcpy(&m_sName, name, strlen(name)*sizeof(char));
}
BookRecord::~BookRecord()
{
}
// Edit: Question grew
char* BookRecord::GetName()
{
    return static_cast<char*>(m_sName);
}
void BookRecord::SetName(char *name)
{
    strcpy(m_sName, name); // TODO: handle null, and sizechecks here to your likings
}
long BookRecord::GetStockNum()
{
    return m_lStockNum;
}
void BookRecord::SetStockNum(long sn)
{
    m_lStockNum = sn;
}
int BookRecord::GetClassification()
{
    return m_iClassification;
}
void BookRecord::SetClassification(int cl)
{
    m_iClassification = cl;
}
double BookRecord::GetCost()
{
    return m_dCost;
}
void BookRecord::SetCost(double c)
{
    m_dCost = c;
}
int BookRecord::GetNumberInStock()
{
    return m_iCount;
}
void BookRecord::SetNumberInStock(int count)
{
    m_iCount = count;
}
void BookRecord::PrintRecord()
{
    cout << static_cast<const char*>(m_sName) << endl;
    cout << "Stock Number: " << m_lStockNum << endl;
    cout << "Class: " << m_iClassification << endl;
    cout << "Cost: $" << m_dCost << endl;
    cout << "In Stock: " << m_iCount << endl;
}

如果你想知道异常抛出,可以看看这篇文章。

希望它能帮助

相关文章: