我不能打印我的书的名字,但可以打印其他所有的东西
I cant print the name of my book but can print everything else
我的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;
}
如果你想知道异常抛出,可以看看这篇文章。
希望它能帮助
相关文章:
- 如何循环打印顶点结构
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号
- 我正在尝试为字符打印一些相应的值,但条件总是转到其他位置
- fprintf 不打印到文件,当文件指针在其他地方声明时
- 在 Eclipse 中漂亮的打印C++适用于某些项目,而不适用于其他项目
- 如何将文件打印到其他文件夹
- C++ 将文件中的数字读取到数组中,然后将其打印到其他数组中
- 在打印出动态分配的数组中的前两个数字时遇到问题,其他数字工作正常
- 如何使用Xcode中的Unicode打印字符╔(和其他字符)
- C GTEST在测试结束时仅在测试失败时打印其他信息
- 当一个线程希望执行或打印某个内容时,如何挂起其他线程
- sprintf,不打印时间(0)和其他值
- 我编写了一个C++程序来打印单词字母的排列.我想知道是否有其他更简单的方法可以做到这一点
- 我不能打印我的书的名字,但可以打印其他所有的东西
- 如果打印后输入整数,则C++分段故障,但其他情况下可以
- 通过杂注消息打印基于其他宏的定义数值?
- 如何在主窗口上打印来自其他类的消息(QString)