【问题标题】:Error: no matching function for call to (unsolvable?)错误:没有匹配的调用函数(无法解决?)
【发布时间】:2017-11-07 00:40:36
【问题描述】:

我是 C++ 初学者。我正在尝试使用结构名称作为模板参数来实例化一个类的三个数组。从那里,我想循环并调用 mutator 函数来设置数组中的值。

int main()
{
    GenericRecord<Furniture> furnObj[10][3];
    GenericRecord<Building> buildObj[10][3];
    GenericRecord<Computer> compObj[10][3];

    Building b;
    Furniture f;
    Computer c;

    for (int i = 0; i < 3; i++)
    {
        cout << "Enter the identifier for the furniture: ";
        cin >> f.Identifier;
        furnObj[i][0].setRecord(f.Identifier);
    }
    return 0;
}

我不断收到如下编译器错误:

error: no matching function for call to 'GenericRecord<Furniture>::setRecord(int&)' note: candidate is: note: void GenericRecord<Type>::setRecord(Type) [with Type = Furniture]

我尝试了 100 种不同的方法来重新设计,但我不断收到不同类型的编译器错误。我做错了什么?

这是模板类、成员和结构:

struct Furniture
{
    int Identifier;
    string Description;
    float Value;
};

template <class Type>
class GenericRecord
{
    private:
        Type record;
    public:
        void setRecord(Type recParam);
};

template<class Type>
void GenericRecord<Type>::setRecord(Type recParam)
{
    record = recParam;
}

【问题讨论】:

  • 在您的情况下,TypeFurniture,但您传递的是 f.Identifier,这是一个 int
  • 不要打电话给furnObj[i][0].setRecord(f.Identifier);,试试furnObj[i][0].setRecord(f);
  • 只是阅读错误信息。说您有一个名为 setRecord 的函数,它以 Type 作为参数,但您使用 int 调用它

标签: c++ templates compiler-errors


【解决方案1】:

(如果你是 C++ 初学者,我认为你不应该直接研究模板,但无论如何......)

GenericRecord<Furniture> furnObj[10][3];

这将创建一个 GenericRecord 对象数组,其中 Type = Furniture。由于 Type = Furniture,这意味着 setRecord(Type recParam) 期望收到一个 Furniture 对象。

但是你用f.Identifier 调用setRecord(),这是一个int。这就是问题的根源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 2012-12-28
    • 2015-03-31
    • 1970-01-01
    • 2016-01-18
    相关资源
    最近更新 更多