Results 1 to 14 of 14

Thread: QT Object created in a private cannot be accessed

  1. #1
    Join Date
    Mar 2015
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default 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

    Qt Code:
    1. #ifndef PRODUCT_H
    2. #define PRODUCT_H
    3.  
    4. #include "Vendor.h"
    5. #include <QString>
    6.  
    7. class Product {
    8.  
    9. private:
    10. QString m_Name;
    11. double m_Price;
    12. //A vendor object
    13. Vendor m_Supplier;
    14.  
    15. public:
    16. Product (QString name, double price);
    17. void setSupplier(QString name, QString email, bool isManufacturer);
    18. QString getManufacturerName();
    19. QString toString(bool supplierDetails);
    20.  
    21. };
    22. #endif // PRODUCT_H
    To copy to clipboard, switch view to plain text mode 

    Vendor.h

    Qt Code:
    1. #ifndef VENDOR_H
    2. #define VENDOR_H
    3.  
    4. #include <QString>
    5.  
    6. class Vendor {
    7.  
    8. private:
    9. QString m_Name;
    10. QString m_Email;
    11. bool m_IsManufacturer;
    12.  
    13. public:
    14. Vendor();
    15. void setDetails(QString name, QString email, bool isManufacturer);
    16. bool isManufacturer();
    17. QString getName();
    18. QString toString();
    19.  
    20. };
    21. #endif // VENDOR_H
    To copy to clipboard, switch view to plain text mode 

    Main.cpp

    Qt Code:
    1. #include <QCoreApplication>
    2. #include "Product.h"
    3. #include"Vendor.h"
    4. #include <QString>
    5. #include <QDebug>
    6. #include <QTextStream>
    7.  
    8. using namespace std;
    9.  
    10. QTextStream cout(stdout);
    11. QTextStream cin(stdin);
    12.  
    13. int main(int argc, char *argv[])
    14. {
    15. QCoreApplication a(argc, argv);
    16.  
    17. QString product_Name;
    18. QString product_Price;
    19. double productPrice;
    20. QString supplier_Name;
    21. QString supplier_Email;
    22. QString supplier_IsManufacturer;
    23. bool supplierIsManufacturer;
    24.  
    25. cout << "Enter Product_Name:" << endl;
    26. product_Name = cin.readLine();
    27. cout << "Enter Product_Price:" << endl;
    28. product_Price = cin.readLine();
    29. cout << "Enter Supplier_Name:" << endl;
    30. supplier_Name = cin.readLine();
    31. cout << "Enter Supplier_Email:" << endl;
    32. supplier_Email = cin.readLine();
    33. cout <<"Is the supplier a manufacturer (Y/N) : " << endl;
    34. supplier_IsManufacturer = cin.readLine();
    35.  
    36. productPrice = product_Price.toDouble();
    37.  
    38. if(supplier_IsManufacturer.at(0).toLower() == 'y') {
    39. supplierIsManufacturer = true;
    40. }
    41.  
    42. Vendor m_Supplier;
    43. Product product (product_Name, productPrice);
    44. m_Supplier.setDetails(supplier_Name, supplier_Email, supplierIsManufacturer);
    45.  
    46. cout << product.toString(supplierIsManufacturer) << endl;
    47.  
    48. return 0;
    49.  
    50. }
    To copy to clipboard, switch view to plain text mode 

    product.cpp

    Qt Code:
    1. #include "Product.h"
    2. #include<QString>
    3.  
    4. Product::Product (QString name, double price) {
    5.  
    6. m_Name = name;
    7. m_Price = price;
    8.  
    9. }
    10.  
    11. void Product::setSupplier(QString name, QString email, bool isManufacturer){
    12.  
    13. m_Supplier.setDetails(name, email, isManufacturer);
    14. }
    15.  
    16. QString Product::getManufacturerName() {
    17.  
    18. if(m_Supplier.isManufacturer()) {
    19. return m_Supplier.getName();
    20. }
    21. else {
    22. return "Unknown";
    23. }
    24. }
    25.  
    26. QString Product::toString(bool supplierDetails){
    27.  
    28. QString dataout;
    29.  
    30. dataout = "\nProduct Name:\t\t" + m_Name + "\nProduct Price:\t\tR" + QString::number(m_Price) + "\nProduct Supplier:\t" + getManufacturerName();
    31.  
    32. if(supplierDetails) {
    33. return m_Supplier.toString() + dataout;
    34. }
    35. else{
    36. return dataout;
    37. }
    38.  
    39.  
    40. }
    To copy to clipboard, switch view to plain text mode 


    Vendor.cpp

    Qt Code:
    1. #include"Vendor.h"
    2. #include<QString>
    3.  
    4.  
    5. Vendor::Vendor() {}
    6.  
    7. void Vendor::setDetails(QString name, QString email, bool isManufacturer){
    8.  
    9. m_Name = name;
    10. m_Email = email;
    11. m_IsManufacturer = isManufacturer;
    12.  
    13. }
    14.  
    15. bool Vendor::isManufacturer (){
    16. return m_IsManufacturer;
    17. }
    18.  
    19. QString Vendor::getName()
    20. {
    21.  
    22. return m_Name;
    23.  
    24. }
    25.  
    26. QString Vendor::toString(){
    27.  
    28. QString dataout;
    29.  
    30. if( m_IsManufacturer ) {
    31. return dataout = "\nVendor name:\t\t" + m_Name + "\nVendor email:\t\t" + m_Email ;
    32.  
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2015
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default 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

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  5. #5
    Join Date
    Mar 2015
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QT Object created in a private cannot be accessed

    Hi ChrisW67,

    Can you explain further how I could resolve that.

    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT Object created in a private cannot be accessed

    Do not create a local variable which shadows the member.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2015
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default 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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT Object created in a private cannot be accessed

    Quote Originally Posted by aquanaut View Post
    What would be the alternative to access the function. I am still new to QT.
    What function?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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?

  10. #10
    Join Date
    Mar 2015
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default 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

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT Object created in a private cannot be accessed

    Remove line #42 from your third snippet.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Mar 2015
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default 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

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT Object created in a private cannot be accessed

    Quote Originally Posted by aquanaut View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QT Object created in a private cannot be accessed

    Quote Originally Posted by aquanaut View Post
    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.

Similar Threads

  1. Access Object Created by javascript in qml
    By alizadeh91 in forum Qt Quick
    Replies: 6
    Last Post: 23rd February 2012, 16:06
  2. Two dynamically created object interaction issue
    By kornicameister in forum Qt Quick
    Replies: 2
    Last Post: 9th September 2011, 12:35
  3. QtScript. How to add created object to script?
    By sergey_85 in forum Qt Programming
    Replies: 1
    Last Post: 18th February 2010, 15:24
  4. Replies: 4
    Last Post: 9th February 2010, 18:08
  5. Replies: 2
    Last Post: 20th September 2009, 03:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.