QT Object created in a private cannot be accessed
Goodday ,
I have created an object in a private class and want to access that object in that classes function. It would seem that it is not working.
When I answer yes to manufacturer the output is missing name, email and product supplier. If I say no then product supplier is still missing.
Enter Product_Name:
test product
Enter Product_Price:
333
Enter Supplier_Name:
shops
Enter Supplier_Email:
email.com
Is the supplier a manufacturer (Y/N) :
y
Vendor name:
Vendor email:
Product Name: test product
Product Price: R333
Product Supplier:
Press <RETURN> to close this window...
Here is my code as I am not sure why I am getting this issue.
Thanks for the help
Product.h
Code:
#ifndef PRODUCT_H
#define PRODUCT_H
#include "Vendor.h"
#include <QString>
class Product {
private:
double m_Price;
//A vendor object
Vendor m_Supplier;
public:
Product
(QString name,
double price
);
QString toString
(bool supplierDetails
);
};
#endif // PRODUCT_H
Vendor.h
Code:
#ifndef VENDOR_H
#define VENDOR_H
#include <QString>
class Vendor {
private:
bool m_IsManufacturer;
public:
Vendor();
bool isManufacturer();
};
#endif // VENDOR_H
Main.cpp
Code:
#include <QCoreApplication>
#include "Product.h"
#include"Vendor.h"
#include <QString>
#include <QDebug>
#include <QTextStream>
using namespace std;
int main(int argc, char *argv[])
{
double productPrice;
bool supplierIsManufacturer;
cout << "Enter Product_Name:" << endl;
product_Name = cin.readLine();
cout << "Enter Product_Price:" << endl;
product_Price = cin.readLine();
cout << "Enter Supplier_Name:" << endl;
supplier_Name = cin.readLine();
cout << "Enter Supplier_Email:" << endl;
supplier_Email = cin.readLine();
cout <<"Is the supplier a manufacturer (Y/N) : " << endl;
supplier_IsManufacturer = cin.readLine();
productPrice = product_Price.toDouble();
if(supplier_IsManufacturer.at(0).toLower() == 'y') {
supplierIsManufacturer = true;
}
Vendor m_Supplier;
Product product (product_Name, productPrice);
m_Supplier.setDetails(supplier_Name, supplier_Email, supplierIsManufacturer);
cout << product.toString(supplierIsManufacturer) << endl;
return 0;
}
product.cpp
Code:
#include "Product.h"
#include<QString>
Product
::Product (QString name,
double price
) {
m_Name = name;
m_Price = price;
}
void Product
::setSupplier(QString name,
QString email,
bool isManufacturer
){
m_Supplier.setDetails(name, email, isManufacturer);
}
QString Product
::getManufacturerName() {
if(m_Supplier.isManufacturer()) {
return m_Supplier.getName();
}
else {
return "Unknown";
}
}
QString Product
::toString(bool supplierDetails
){
dataout
= "\nProduct Name:\t\t" + m_Name
+ "\nProduct Price:\t\tR" + QString::number(m_Price
) + "\nProduct Supplier:\t" + getManufacturerName
();
if(supplierDetails) {
return m_Supplier.toString() + dataout;
}
else{
return dataout;
}
}
Vendor.cpp
Code:
#include"Vendor.h"
#include<QString>
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;
}
{
return m_Name;
}
if( m_IsManufacturer ) {
return dataout = "\nVendor name:\t\t" + m_Name + "\nVendor email:\t\t" + m_Email ;
}
}
Re: QT Object created in a private cannot be accessed
What is the question? How is the code you posted related to the output and to the description from the beginning of the post?
Re: QT Object created in a private cannot be accessed
I think I am having a problem acessing the object that is why I am not getting the required out. I need some advice as to whether I am looking in the right place for the problem or if it is another issue that is causing the output not to appear.
Thanks
Re: QT Object created in a private cannot be accessed
No, you have a problem setting a value on the Vendor member variable of the Product class... You never do that.
Re: QT Object created in a private cannot be accessed
Hi ChrisW67,
Can you explain further how I could resolve that.
Thanks
Re: QT Object created in a private cannot be accessed
Do not create a local variable which shadows the member.
Re: QT Object created in a private cannot be accessed
What would be the alternative to access the function. I am still new to QT.
Thanks
Re: QT Object created in a private cannot be accessed
Quote:
Originally Posted by
aquanaut
What would be the alternative to access the function. I am still new to QT.
What function?
Re: QT Object created in a private cannot be accessed
As everyone has pointed out, you are never setting the Vendor info for the private variable m_Supplier in your Product class.
You do realize that the Vendor variable instanced named m_Supplier that you create in main.cpp on line 42, is not the same instance as the private member variable in your product class, correct?
Re: QT Object created in a private cannot be accessed
I see the problem but I need some guidance to fix it. What steps do I follow to fix the problem.
Thanks
Re: QT Object created in a private cannot be accessed
Remove line #42 from your third snippet.
Re: QT Object created in a private cannot be accessed
I removed line #42 and get the following error : 44: error: 'm_Supplier' was not declared in this scope m_Supplier.setDetails(supplier_Name, supplier_Email, supplierIsManufacturer);
So I removed line #44 as well but still have the same output problem.
I am using QT 5.3
Thanks
Re: QT Object created in a private cannot be accessed
Quote:
Originally Posted by
aquanaut
I removed line #42 and get the following error : 44: error: 'm_Supplier' was not declared in this scope m_Supplier.setDetails(supplier_Name, supplier_Email, supplierIsManufacturer);
Good. Now fix your code so that it logically does what you want. Bear in mind the supplier is a member variable in the product class.
BTW. I'm moving this to General Programming as the problem has nothing to do with Qt.
Re: QT Object created in a private cannot be accessed
Quote:
Originally Posted by
aquanaut
Can you explain further how I could resolve that.
Well, you gave your Product class a public function that can be used to set the value stored in it Vendor member variable.