值在 Qt 程序中丢失并出现 类 输出错误
Values getting lost in Qt Program with Classes output error
我有一个 Qt 作业,但我似乎无法开始工作。
这是作业说明:
A product is described using a name, a price and a supplier. The
supplier and the manufacturer can be the same for a product.
A Product
is initialised using a name and a price. The supplier details are set using the function setSupplier()
, which invokes the
suitable function of Vendor
to set the details of the vendor. If
one requests a product for its manufacturer, it returns the name of
the vendor stored in m_Supplier
, if it is set as the manufacturer.
Otherwise an Unknown
string is returned. toString()
of Vendor
returns a string representation of the values of its data members in a
readable format. toString()
of Product
always returns a string
representation of a product. If supplierDetails
in toString()
of
Product
is true
, it returns both the supplier and product details.
Otherwise only product details are returned.
Implement it as a console application, where the product and suuplier
details are entered from the keyboard. Display the results of
getManufacturerName()
and toString()
(for both true
and false
parameter values) on the console.
不幸的是,我真的找不到哪里出了问题,所以我将在下面列出我所有文件的所有代码,希望有人能发现哪里出了问题。
main.cpp
#include <QCoreApplication>
#include "vendor.h"
#include "product.h"
#include <QString>
#include <QTextStream>
#include <QDebug>
QTextStream cout(stdout);
QTextStream cin(stdin);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//Declare variables
QString productName;
QString productPriceStr;
double productPrice;
QString supplierName;
QString supplierEmail;
QString supplierIsManufacturerStr;
bool supplierIsManufacturer;
//Get user input
cout << "Enter the product name:" << endl;
productName = cin.readLine();
cout << "Enter the product price:" << endl;
productPriceStr = cin.readLine();
cout << "Enter the supplier name:" << endl;
supplierName = cin.readLine();
cout << "Enter the supplier email:" << endl;
supplierEmail = cin.readLine();
cout <<"Is the supplier a manufacturer (y/n):\t" << endl;
supplierIsManufacturerStr = cin.readLine();
productPrice = productPriceStr.toDouble();
if(supplierIsManufacturerStr.at(0).toLower() == 'y') {
supplierIsManufacturer = true;
} else {
supplierIsManufacturer = false;
}
//Implement classes
Vendor vendor;
Product product(productName, productPrice, vendor);
product.setSupplier(supplierName, supplierEmail, supplierIsManufacturer);
cout << product.toString(supplierIsManufacturer) << endl;
return a.exec();
}
product.h
#ifndef PRODUCT_H
#define PRODUCT_H
#include "vendor.h"
#include <QString>
class Product {
public:
Product(QString name, double price, Vendor vendor);
void setSupplier(QString name, QString email, bool isManufacturer);
QString getManufacturerName();
QString toString(bool SupplierDetails);
private:
QString m_Name;
double m_Price;
Vendor m_Supplier;
};
#endif // PRODUCT_H
product.cpp
#include <QString>
#include "product.h"
#include "vendor.h"
Product::Product(QString name, double price, Vendor vendor) {
m_Name = name;
m_Price = price;
m_Supplier = vendor;
}
void Product::setSupplier(QString name, QString email, bool isManufacturer) {
Vendor vendor;
vendor.setDetails(name, email, isManufacturer);
}
QString Product::getManufacturerName() {
if(m_Supplier.isManufacturer()) {
return m_Supplier.getName();
} else {
return "Unknown";
}
}
QString Product::toString(bool supplierDetails) {
QString output;
output = "\nProduct name:\t\t" + m_Name + "\nProduct price:\t\tR" + m_Price + "\nProduct Supplier:\t" + getManufacturerName();
if(supplierDetails) {
return m_Supplier.toString() + output;
} else {
return output;
}
}
vendor.h
#ifndef VENDOR_H
#define VENDOR_H
#include <QString>
class Vendor {
public:
Vendor();
void setDetails(QString name, QString email, bool isManufacturer);
bool isManufacturer();
QString getName();
QString toString();
private:
QString m_Name;
QString m_Email;
bool m_IsManufacturer;
};
#endif // VENDOR_H
vendor.cpp
#include <QString>
#include "vendor.h"
Vendor::Vendor() {
}
void Vendor::setDetails(QString name, QString email, bool isManufacturer) {
m_Name = name;
m_Email = email;
m_IsManufacturer = isManufacturer;
}
bool Vendor::isManufacturer() {
return m_IsManufacturer;
}
QString Vendor::getName() {
return m_Name;
}
QString Vendor::toString() {
QString output;
QString manufacturerYesNo;
if(m_IsManufacturer) {
manufacturerYesNo = "Yes";
} else {
manufacturerYesNo = "No";
}
output = "\nVendor name:\t\t" + m_Name + "\nVendor email:\t\t" + m_Email + "\nManufacturer:\t\t" + manufacturerYesNo;
return output;
}
就是这样。
没有编译错误,这纯粹是算法错误。到目前为止,无论我给它输入什么,它总是只显示产品名称,有时还会显示产品价格的非数值,然后其他所有内容都是空的。 toString()
函数似乎仍然 运行。
如果有人能看出问题所在,你就是救星!
这一行
output = "\nProduct name:\t\t" + m_Name + "\nProduct price:\t\tR" + m_Price + "\nProduct Supplier:\t" + getManufacturerName();
应该是:
output = "\nProduct name:\t\t" + m_Name + "\nProduct price:\t\tR" + QString::number(m_Price) + "\nProduct Supplier:\t" + getManufacturerName();
您应该在“+”之前将数字转换为 QString。
并在供应商中重载运算符“=”class
Vendor operator=(const Vendor& other)
{
m_Name = other.m_Name;
m_Email = other.m_Email;
m_IsManufacturer = other.m_IsManufacturer;
}
我知道出了什么问题。在我的 product.cpp 文件中,我在 setDetails 函数中创建了一个新的供应商,这让我的所有供应商详细信息都留空了。我应该使用我在 Product 构造函数中设置为参数的 m_Supplier 变量...
我有一个 Qt 作业,但我似乎无法开始工作。
这是作业说明:
A product is described using a name, a price and a supplier. The supplier and the manufacturer can be the same for a product.
A
Product
is initialised using a name and a price. The supplier details are set using the functionsetSupplier()
, which invokes the suitable function ofVendor
to set the details of the vendor. If one requests a product for its manufacturer, it returns the name of the vendor stored inm_Supplier
, if it is set as the manufacturer. Otherwise anUnknown
string is returned.toString()
ofVendor
returns a string representation of the values of its data members in a readable format.toString()
ofProduct
always returns a string representation of a product. IfsupplierDetails
intoString()
ofProduct
istrue
, it returns both the supplier and product details. Otherwise only product details are returned.Implement it as a console application, where the product and suuplier details are entered from the keyboard. Display the results of
getManufacturerName()
andtoString()
(for bothtrue
andfalse
parameter values) on the console.
不幸的是,我真的找不到哪里出了问题,所以我将在下面列出我所有文件的所有代码,希望有人能发现哪里出了问题。
main.cpp
#include <QCoreApplication>
#include "vendor.h"
#include "product.h"
#include <QString>
#include <QTextStream>
#include <QDebug>
QTextStream cout(stdout);
QTextStream cin(stdin);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//Declare variables
QString productName;
QString productPriceStr;
double productPrice;
QString supplierName;
QString supplierEmail;
QString supplierIsManufacturerStr;
bool supplierIsManufacturer;
//Get user input
cout << "Enter the product name:" << endl;
productName = cin.readLine();
cout << "Enter the product price:" << endl;
productPriceStr = cin.readLine();
cout << "Enter the supplier name:" << endl;
supplierName = cin.readLine();
cout << "Enter the supplier email:" << endl;
supplierEmail = cin.readLine();
cout <<"Is the supplier a manufacturer (y/n):\t" << endl;
supplierIsManufacturerStr = cin.readLine();
productPrice = productPriceStr.toDouble();
if(supplierIsManufacturerStr.at(0).toLower() == 'y') {
supplierIsManufacturer = true;
} else {
supplierIsManufacturer = false;
}
//Implement classes
Vendor vendor;
Product product(productName, productPrice, vendor);
product.setSupplier(supplierName, supplierEmail, supplierIsManufacturer);
cout << product.toString(supplierIsManufacturer) << endl;
return a.exec();
}
product.h
#ifndef PRODUCT_H
#define PRODUCT_H
#include "vendor.h"
#include <QString>
class Product {
public:
Product(QString name, double price, Vendor vendor);
void setSupplier(QString name, QString email, bool isManufacturer);
QString getManufacturerName();
QString toString(bool SupplierDetails);
private:
QString m_Name;
double m_Price;
Vendor m_Supplier;
};
#endif // PRODUCT_H
product.cpp
#include <QString>
#include "product.h"
#include "vendor.h"
Product::Product(QString name, double price, Vendor vendor) {
m_Name = name;
m_Price = price;
m_Supplier = vendor;
}
void Product::setSupplier(QString name, QString email, bool isManufacturer) {
Vendor vendor;
vendor.setDetails(name, email, isManufacturer);
}
QString Product::getManufacturerName() {
if(m_Supplier.isManufacturer()) {
return m_Supplier.getName();
} else {
return "Unknown";
}
}
QString Product::toString(bool supplierDetails) {
QString output;
output = "\nProduct name:\t\t" + m_Name + "\nProduct price:\t\tR" + m_Price + "\nProduct Supplier:\t" + getManufacturerName();
if(supplierDetails) {
return m_Supplier.toString() + output;
} else {
return output;
}
}
vendor.h
#ifndef VENDOR_H
#define VENDOR_H
#include <QString>
class Vendor {
public:
Vendor();
void setDetails(QString name, QString email, bool isManufacturer);
bool isManufacturer();
QString getName();
QString toString();
private:
QString m_Name;
QString m_Email;
bool m_IsManufacturer;
};
#endif // VENDOR_H
vendor.cpp
#include <QString>
#include "vendor.h"
Vendor::Vendor() {
}
void Vendor::setDetails(QString name, QString email, bool isManufacturer) {
m_Name = name;
m_Email = email;
m_IsManufacturer = isManufacturer;
}
bool Vendor::isManufacturer() {
return m_IsManufacturer;
}
QString Vendor::getName() {
return m_Name;
}
QString Vendor::toString() {
QString output;
QString manufacturerYesNo;
if(m_IsManufacturer) {
manufacturerYesNo = "Yes";
} else {
manufacturerYesNo = "No";
}
output = "\nVendor name:\t\t" + m_Name + "\nVendor email:\t\t" + m_Email + "\nManufacturer:\t\t" + manufacturerYesNo;
return output;
}
就是这样。
没有编译错误,这纯粹是算法错误。到目前为止,无论我给它输入什么,它总是只显示产品名称,有时还会显示产品价格的非数值,然后其他所有内容都是空的。 toString()
函数似乎仍然 运行。
如果有人能看出问题所在,你就是救星!
这一行
output = "\nProduct name:\t\t" + m_Name + "\nProduct price:\t\tR" + m_Price + "\nProduct Supplier:\t" + getManufacturerName();
应该是:
output = "\nProduct name:\t\t" + m_Name + "\nProduct price:\t\tR" + QString::number(m_Price) + "\nProduct Supplier:\t" + getManufacturerName();
您应该在“+”之前将数字转换为 QString。
并在供应商中重载运算符“=”class
Vendor operator=(const Vendor& other)
{
m_Name = other.m_Name;
m_Email = other.m_Email;
m_IsManufacturer = other.m_IsManufacturer;
}
我知道出了什么问题。在我的 product.cpp 文件中,我在 setDetails 函数中创建了一个新的供应商,这让我的所有供应商详细信息都留空了。我应该使用我在 Product 构造函数中设置为参数的 m_Supplier 变量...