简体   繁体   中英

C++ ERROR No matching function for call

I've been working on a Qt Assignment and I've completed the whole task but I can't seem to fix one final bug that won't let my program run.

The error is:

no matching function for call to 'Vendor::Vendor(QString&, QString&, bool&)'
  Vendor supplierInfo(supplierNmae,supplierEmail,supplierIsManufacturer);
  line 41                                                              ^

It consist of two classes but only my vendor class and main class is affected by the error.

Here is the code to my main.cpp:

#include <QCoreApplication>
#include "vendor.h"
#include "product.h"
#include <QString>
#include <QTextStream>

QTextStream cout(stdout);
QTextStream cin(stdin);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //Declare variables
    QString productName;
    double productPrice;
    QString supplierName;
    QString supplierEmail;
    QString supplierIsManufacturerStr;
    bool supplierIsManufacturer;

    //Get user input
    cout << "Enter the product name:\t";
    productName = cin.readLine();
    cout << "Enter the product price:\tR";
    cin >> productPrice;
    cout << "Enter the supplier name:\t";
    supplierName = cin.readLine();
    cout << "Enter the supplier email:\t";
    cin >> supplierEmail;
    cout <<"Is the supplier a manufacturer:\t";
    cin >> supplierIsManufacturerStr;

    if(supplierIsManufacturerStr.at(0).toLower() == 'y') {
        supplierIsManufacturer = true;
    } else {
        supplierIsManufacturer = false;
    }

    //Implement classes
    Vendor vendor(supplierName, supplierEmail, supplierIsManufacturer);
    Product product(productName, productPrice, vendor);
    product.setSupplier(supplierName, supplierEmail, supplierIsManufacturer);

    product.toString(supplierIsManufacturer);

    return a.exec();
}

And here's the code to my vendor.h file:

#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

I'm still getting back into the zone regarding using classes so I'm quite out of practice and would appreciate any help I could get on this. Thank you in advance.

The Vendor class is missing a constructor matching the line in main.cpp. Either implement the constructor:

// Vendor.h
class Vendor {
public:
  Vendor();
  Vendor(const QString& name, const QString& email, bool isManufacturer);
  ...

// Vendor.cpp
Vendor::Vendor(const QString& name, const QString& email, bool isManufacturer)
: m_Name(name), m_Email(email), m_IsManufacturer(isManufacturer)
{}

Or use the existing setDetails function:

Vendor vendor;
vendor.setDetails(supplierName, supplierEmail, supplierIsManufacturer);

供应商中的初始值设定项功能需要重写,并将其参数传递给setDetails

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM