C++错误:"没有与所需类型匹配的函数模板实例"
C++ error: "no instance of function template matches the required type"
我是一个C++新手,正在编写一个类来对多项式进行加法、减法和乘法运算。我看到这个错误:
"没有与所需类型匹配的功能模板实例">
由以下语句产生:
display(add, count);
display(sub, count);
display(mult, count);
P1.display(add, count); (as well as for sub and mult)
P2.display(add, count); (as well as for sub and mult)
我的代码是:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
class Poly
{
private:
int order; // the order of the polynomial
int *coeff; // pointer to an array of coefficients
// size of the coefficient array is predicated on [order + 1]
int *add;
int *sub;
int *mult;
public:
// Poly(); // the default constructor
int setOrderAndCoeff(); // sets the order and coefficients
int display(int *data, int count); // displays the resutling polynomial
void addition(Poly P1, Poly P2); // adds 2 polynomials
void subtraction (Poly P1, Poly P2); // subtracts 2 polynomials
void multiplication (Poly P1, Poly P2); // multiplies 2 polynomials
// ~Poly(); // the destructor
};
int Poly::display(int *data, int count)
{
for (int i = count; i >= 0; i--)
{
cout << data[i] << "x^" << i;
if ((i - 1) != -1)
{
cout << "+";
}
}
cout << "n";
return 0;
}
int Poly::setOrderAndCoeff()
{
int i;
cout << "Please enter the order of the polynomial: ";
cin >> order;
coeff = new int[order + 1];
for (i = order; i >= 0; i--)
{
cout << "Please enter the coefficient of x^" << i << " :";
cin >> coeff[i];
}
return 0;
}
void Poly::addition(Poly P1, Poly P2)
{
int max;
int i;
max = (P1.order > P2.order) ? P1.order : P2.order;
add = new int [max + 1];
if (P1.order == P2.order)
{
for (i = P1.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
if (P1.order > P2.order)
{
for (i = P1.order; i > P2.order; i--)
{
add[i] = P1.coeff[i];
}
for (i = P2.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
if (P1.order < P2.order)
{
for (i = P2.order; i > P1.order; i--)
{
add[i] = P2.coeff[i];
}
for (i = P1.order; i >= 0; i--)
{
add[i] = P1.coeff[i] + P2.coeff[i];
}
}
cout << "nAddition:";
display(add, count);
cout << "n";
}
void Poly::subtraction(Poly P1, Poly P2)
{
int max;
int i;
max = (P1.order > P2.order) ? P1.order : P2.order;
int *sub = new int[max + 1];
if (P1.order == P2.order)
{
for (i = P1.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
if (P1.order > P2.order)
{
for (i = P1.order; i > P2.order; i--)
{
sub[i] = P1.coeff[i];
}
for (i = P2.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
if (P1.order < P2.order)
{
for (i = P2.order; i > P1.order; i--)
{
sub[i] = -P2.coeff[i];
}
for (i = P1.order; i >= 0; i--)
{
sub[i] = P1.coeff[i] - P2.coeff[i];
}
}
cout << "nSubtraction:";
display(sub, count);
cout << "n";
}
void Poly::multiplication(Poly P1, Poly P2)
{
int i;
int j;
int max;
max = P1.order + P2.order;
int *mult = new int[max + 1];
for (i = P1.order; i >= 0; i--)
for (j = P2.order; j >= 0; j--)
{
mult[i + j] += P1.coeff[i] * P2.coeff[i];
}
cout << "nMultiplication:";
display(mult, count);
}
int main()
{
int choice;
Poly P1, P2, P3;
cout << "-------- Instructions --------" << endl;
cout << "For polynomial 1... " << endl;
P1.setOrderAndCoeff();
cout << endl;
cout << "For polynomial 2... " << endl;
P2.setOrderAndCoeff();
while (1)
{
cout << "n******** Menu Selection ********" << endl;
cout << "1: Additionn2: Subtractionn3: Mutiplicationn0: Exit" << endl;
cout << "Please enter your choice (1, 2, 3 or 0):";
cin >> choice;
switch (choice)
{
case 1:
cout << "n-------- Addition --------n";
cout << "Polynomial 1: ";
P1.display(add, count);
cout << "Polynomial 2: ";
P2.display(add, count);
P3.addition(P1, P2);
cout << "--------------------------n";
break;
case 2:
cout << "n-------- Subtraction --------n";
cout << "Polynomial 1: ";
P1.display(sub, count);
cout << "Polynomial 2: ";
P2.display(sub, count);
P3.subtraction(P1, P2);
cout << "--------------------------n";
break;
case 3:
cout << "n-------- Multiplication --------n";
cout << "Polynomial 1: ";
P1.display(mult, count);
cout << "Polynomial 2: ";
P2.display(mult, count);
P3.multiplication(P1, P2);
cout << "--------------------------n";
break;
case 0:
cout << "The program will now terminate. Thank you." << endl;
exit(0);
default:
cout << endl;
cout << "You have entered an invalid selection." << endl;
cout << "Please enter a positive integer between 0 and 3.";
cout << endl;
}
}
return 0;
有人知道是什么原因造成的吗?
谢谢你的指导?-Ryan
在这个代码片段中
switch (choice)
{
case 1:
cout << "n-------- Addition --------n";
cout << "Polynomial 1: ";
P1.display(add, count);
cout << "Polynomial 2: ";
P2.display(add, count);
P3.addition(P1, P2);
cout << "--------------------------n";
break;
case 2:
cout << "n-------- Subtraction --------n";
cout << "Polynomial 1: ";
P1.display(sub, count);
cout << "Polynomial 2: ";
P2.display(sub, count);
P3.subtraction(P1, P2);
cout << "--------------------------n";
break;
case 3:
cout << "n-------- Multiplication --------n";
cout << "Polynomial 1: ";
P1.display(mult, count);
cout << "Polynomial 2: ";
P2.display(mult, count);
P3.multiplication(P1, P2);
cout << "--------------------------n";
break;
case 0:
cout << "The program will now terminate. Thank you." << endl;
exit(0);
default:
cout << endl;
cout << "You have entered an invalid selection." << endl;
cout << "Please enter a positive integer between 0 and 3.";
cout << endl;
}
在函数display的所有调用中,既没有定义第一个参数,也没有定义第二个参数。例如add
和count
主要定义在哪里?
P1.display(add, count);
该函数定义为具有两个参数
int Poly::display(int *data, int count)
如果您想作为第一个参数传递,例如数据成员add
,那么您应该至少编写
P1.display( P1.add, count);
使数据成员添加公开。
但是,我看不出名称为count
的第二个参数是在哪里定义的。
例如,在成员函数中,void Poly::addition(Poly P1,Poly P2(;count可以定义为表达式max + 1
,但在main中,您可以在类的其他成员函数之外调用display。因此,我可以得出结论,该代码总体上是错误的。
这当然不是一个好的错误消息。
问题是,当您从main()
调用Poly::display()
时,您传递的是Poly
、add
、mult
和count
的成员的名称,这些名称在主方法中不可见,因为它们在类中被声明为private。
要解决此问题,您需要:
- 定义一个方法,该方法显示所需的成员,而不必从类外传递对它们的引用(例如
displayMult()
等( - 将当前在
main()
中的主循环移动到那些字段可见的位置,或者移动到Poly
的成员中,或者移动Poly
的friend
函数中 - 添加公共getter以检索
Poly
中那些成员变量的值
int setOrderAndCoeff()
成员方法将返回一个整数。但是,在您的主要功能中,
int choice;
Poly P1, P2, P3;
cout << "-------- Instructions --------" << endl;
cout << "For polynomial 1... " << endl;
P1.setOrderAndCoeff(); '<= this is valid as long as there exists a setOrderAndCoeff overloading, return type is void.
我认为修复错误的一种方法是更改
int setOrderAndCoeff()
至
void setOrderAndCoeff(){
int i;
cout << "Please enter the order of the polynomial: ";
cin >> order;
coeff = new int[order + 1];
for (i = order; i >= 0; i--)
{
cout << "Please enter the coefficient of x^" << i << " :";
cin >> coeff[i];
}
}
需要对类中的Display成员方法进行类似的更改。
相关文章:
- 仅在函数模板中为那些定义了函数的类型执行函数
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- 函数模板返回类型
- 特定类型的模板函数的专用化
- 在实例化之前推断函数模板的返回类型
- 函数模板签名中忽略的成员类型def 的访问说明符
- 实例化多种类型的成员函数模板
- 使用函数模板推导返回类型
- 重载模板<类型名...>类的函数模板
- 将函数类型作为模板参数传递不会编译
- 专用于类型集的函数模板
- async中没有匹配的函数(模板中未解析的类型)
- 在C++17中,为什么类模板和函数模板的指针类型推导明显不一致
- 为什么重载的函数模板顺序对基本类型很重要
- 没有函数模板的实例"max"与参数列表参数类型匹配(int、int)
- 模板函数重载(泛型类型与模板模板类型)选择正确的重载
- 如何在返回类型函数模板的专用化中使用派生类型?( "couldn't infer template argument" )
- 具有指针数据类型的非类型函数模板参数
- C++模板基类的非类型函数模板的 using 声明
- 非类型函数模板参数