C ++ ERROR没有匹配的调用函数

时间:2015-02-28 10:40:59

标签: c++ qt function class

我一直致力于Qt作业并完成了整个任务,但我似乎无法修复一个不会让我的程序运行的最终错误。

错误是:

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

它由两个类组成,但只有我的供应商类和主类受错误影响。

以下是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();
}

这是我的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

我仍然会回到关于使用课程的区域,所以我完全没有练习,并希望得到任何帮助。提前谢谢。

2 个答案:

答案 0 :(得分:1)

Vendor类缺少与main.cpp中的行匹配的构造函数。 实现构造函数:

// 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)
{}

或使用现有的setDetails功能:

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

答案 1 :(得分:0)

Vendor中的初始化函数需要一个覆盖,将其参数传递给setDetails