【发布时间】: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;
}
【问题讨论】:
-
在您的情况下,
Type是Furniture,但您传递的是f.Identifier,这是一个int。 -
不要打电话给
furnObj[i][0].setRecord(f.Identifier);,试试furnObj[i][0].setRecord(f); -
只是阅读错误信息。说您有一个名为
setRecord的函数,它以Type作为参数,但您使用 int 调用它
标签: c++ templates compiler-errors